使用jsonpath从kettle转换json2csv
保存配置后,運(yùn)行即可。
對應(yīng)的解析json也類似xml的解析:
一:如果是文件數(shù)據(jù)的話,選擇對應(yīng)的文件;如果是流利的字段,配置好相應(yīng)的字段即可。
二:配置好路徑,讀取json即可。
解析json的具體示例如下:
使用jsonpath解析json內(nèi)容
JsonPath提供的json解析非常強(qiáng)大,它提供了類似正則表達(dá)式的語法,基本上可以滿足所有你想要獲得的json內(nèi)容。下面我把官網(wǎng)介紹的每個表達(dá)式用代碼實現(xiàn),可以更直觀的知道該怎么用它。
一.首先需要依賴的jar包
二.因為編譯的時候會報log4j的警報,所以需要在項目的src目錄下新建log4j.properties文件,內(nèi)容如下:
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
三.準(zhǔn)備一個json文本
{"store": {"book": [{"category": "reference","author": "Nigel Rees","title": "Sayings of the Century","price": 8.95},{"category": "fiction","author": "Evelyn Waugh","title": "Sword of Honour","price": 12.99},{"category": "fiction","author": "Herman Melville","title": "Moby Dick","isbn": "0-553-21311-3","price": 8.99},{"category": "fiction","author": "J. R. R. Tolkien","title": "The Lord of the Rings","isbn": "0-395-19395-8","price": 22.99}],"bicycle": {"color": "red","price": 19.95}},"expensive": 10 }四.我們從代碼中分析用法,代碼如下
package com.jsonpath;import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Iterator; import java.util.List; import java.util.Map;import net.minidev.json.JSONArray;import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.ReadContext;/** * @author QiaoJiafei * @version 創(chuàng)建時間:2016年3月2日 下午3:37:42 * 類說明 */ public class TestJsonPath {public static void main(String[] args) {// TODO Auto-generated method stubString sjson = readtxt();//String sjson = "{\"store\": {\"book\": [{\"category\": \"reference\",\"author\": \"Nigel Rees\",\"title\": \"Sayings of the Century\",\"price\": 8.95},{\"category\": \"fiction\",\"author\": \"Evelyn Waugh\",\"title\": \"Sword of Honour\",\"price\": 12.99},{\"category\": \"fiction\",\"author\": \"Herman Melville\",\"title\": \"Moby Dick\",\"isbn\": \"0-553-21311-3\",\"price\": 8.99},{\"category\": \"fiction\",\"author\": \"J. R. R. Tolkien\",\"title\": \"The Lord of the Rings\",\"isbn\": \"0-395-19395-8\",\"price\": 22.99}],\"bicycle\": {\"color\": \"red\",\"price\": 19.95}},\"expensive\": 10}";/*print("--------------------------------------getJsonValue0--------------------------------------");getJsonValue0(sjson);print("--------------------------------------getJsonValue1--------------------------------------");getJsonValue1(sjson);print("--------------------------------------getJsonValue2--------------------------------------");getJsonValue2(sjson);print("--------------------------------------getJsonValue3--------------------------------------");getJsonValue3(sjson);print("--------------------------------------getJsonValue4--------------------------------------");getJsonValue4(sjson);*/print("--------------------------------------getJsonValue--------------------------------------");getJsonValue(sjson);}private static String readtxt() {// TODO Auto-generated method stubStringBuilder sb = new StringBuilder();try {FileReader fr = new FileReader("D:/workspace/PressureTest/json.txt");BufferedReader bfd = new BufferedReader(fr);String s = "";while((s=bfd.readLine())!=null) {sb.append(s);}} catch (Exception e) {// TODO Auto-generated catch block e.printStackTrace();}System.out.println(sb.toString());return sb.toString();}private static void getJsonValue(String json) {//The authors of all books:獲取json中store下book下的所有author值List<String> authors1 = JsonPath.read(json, "$.store.book[*].author");//All authors:獲取所有json中所有author的值List<String> authors2 = JsonPath.read(json, "$..author");//All things, both books and bicycles //authors3返回的是net.minidev.json.JSONArray:獲取json中store下的所有value值,不包含key,如key有兩個,book和bicycleList<Object> authors3 = JsonPath.read(json, "$.store.*");//The price of everything:獲取json中store下所有price的值List<Object> authors4 = JsonPath.read(json, "$.store..price");//The third book:獲取json中book數(shù)組的第3個值List<Object> authors5 = JsonPath.read(json, "$..book[2]");//The first two books:獲取json中book數(shù)組的第1和第2兩個個值List<Object> authors6 = JsonPath.read(json, "$..book[0,1]");//All books from index 0 (inclusive) until index 2 (exclusive):獲取json中book數(shù)組的前兩個區(qū)間值List<Object> authors7 = JsonPath.read(json, "$..book[:2]");//All books from index 1 (inclusive) until index 2 (exclusive):獲取json中book數(shù)組的第2個值List<Object> authors8 = JsonPath.read(json, "$..book[1:2]");//Last two books:獲取json中book數(shù)組的最后兩個值List<Object> authors9 = JsonPath.read(json, "$..book[-2:]");//Book number two from tail:獲取json中book數(shù)組的第3個到最后一個的區(qū)間值List<Object> authors10 = JsonPath.read(json, "$..book[2:]");//All books with an ISBN number:獲取json中book數(shù)組中包含isbn的所有值List<Object> authors11 = JsonPath.read(json, "$..book[?(@.isbn)]");//All books in store cheaper than 10:獲取json中book數(shù)組中price<10的所有值List<Object> authors12 = JsonPath.read(json, "$.store.book[?(@.price < 10)]");//All books in store that are not "expensive":獲取json中book數(shù)組中price<=expensive的所有值List<Object> authors13 = JsonPath.read(json, "$..book[?(@.price <= $['expensive'])]");//All books matching regex (ignore case):獲取json中book數(shù)組中的作者以REES結(jié)尾的所有值(REES不區(qū)分大小寫)List<Object> authors14 = JsonPath.read(json, "$..book[?(@.author =~ /.*REES/i)]");//Give me every thing:逐層列出json中的所有值,層級由外到內(nèi)List<Object> authors15 = JsonPath.read(json, "$..*");//The number of books:獲取json中book數(shù)組的長度List<Object> authors16 = JsonPath.read(json, "$..book.length()");print("**************authors1**************");print(authors1);print("**************authors2**************");print(authors2);print("**************authors3**************");printOb(authors3);print("**************authors4**************");printOb(authors4);print("**************authors5**************");printOb(authors5);print("**************authors6**************");printOb(authors6);print("**************authors7**************");printOb(authors7);print("**************authors8**************");printOb(authors8);print("**************authors9**************");printOb(authors9);print("**************authors10**************");printOb(authors10);print("**************authors11**************");printOb(authors11);print("**************authors12**************");printOb(authors12);print("**************authors13**************");printOb(authors13);print("**************authors14**************");printOb(authors14);print("**************authors15**************");printOb(authors15);print("**************authors16**************");printOb(authors16);}/*** 讀取json的一種寫法,得到匹配表達(dá)式的所有值* */private static void getJsonValue0(String json) {// TODO Auto-generated method stubList<String> authors = JsonPath.read(json, "$.store.book[*].author");//System.out.println(authors.size()); print(authors);}/*** 讀取json的一種寫法,得到某個具體值* */private static void getJsonValue1(String json) {Object document = Configuration.defaultConfiguration().jsonProvider().parse(json);String author0 = JsonPath.read(document, "$.store.book[0].author");String author1 = JsonPath.read(document, "$.store.book[1].author");print(author0);print(author1);}/*** 讀取json的一種寫法* */private static void getJsonValue2(String json) {ReadContext ctx = JsonPath.parse(json);List<String> authorsOfBooksWithISBN = ctx.read("$.store.book[?(@.isbn)].author");List<Map<String, Object>> expensiveBooks = JsonPath.using(Configuration.defaultConfiguration()).parse(json).read("$.store.book[?(@.price > 10)]", List.class);print(authorsOfBooksWithISBN);print("****************Map****************");printListMap(expensiveBooks);}/***讀取json的一種寫法*得到的值是一個String,所以不能用List存儲 * */private static void getJsonValue3(String json) {//Will throw an java.lang.ClassCastException //List<String> list = JsonPath.parse(json).read("$.store.book[0].author");//由于會拋異常,暫時注釋上面一行,要用的話,應(yīng)使用下面的格式//Works fineString author = JsonPath.parse(json).read("$.store.book[0].author");print(author);}/***讀取json的一種寫法*支持邏輯表達(dá)式,&&和|| * */private static void getJsonValue4(String json) {List<Map<String, Object>> books1 = JsonPath.parse(json).read("$.store.book[?(@.price < 10 && @.category == 'fiction')]");List<Map<String, Object>> books2 = JsonPath.parse(json).read("$.store.book[?(@.category == 'reference' || @.price > 10)]");print("****************books1****************");printListMap(books1);print("****************books2****************");printListMap(books1);}private static void print(List<String> list) {for(Iterator<String> it = list.iterator();it.hasNext();) {System.out.println(it.next());}}private static void printOb(List<Object> list) {for(Iterator<Object> it = list.iterator();it.hasNext();) {print("****");System.out.println(it.next());}}private static void printListMap(List<Map<String, Object>> list) {for(Iterator<Map<String, Object>> it = list.iterator();it.hasNext();) {Map<String, Object> map = it.next();print("****");for(Iterator iterator =map.entrySet().iterator();iterator.hasNext();) {System.out.println(iterator.next());}}}private static void print(String s) {System.out.println(s);}}?
1.首先我們看getJsonValue()這個方法的輸出結(jié)果:
--------------------------------------getJsonValue-------------------------------------- **************authors1************** Nigel Rees Evelyn Waugh Herman Melville J. R. R. Tolkien **************authors2************** Nigel Rees Evelyn Waugh Herman Melville J. R. R. Tolkien **************authors3************** [{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}] {color=red, price=19.95} **************authors4************** 8.95 12.99 8.99 22.99 19.95 **************authors5************** {category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99} **************authors6************** {category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95} {category=fiction, author=Evelyn Waugh, title=Sword of Honour, price=12.99} **************authors7************** {category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95} {category=fiction, author=Evelyn Waugh, title=Sword of Honour, price=12.99} **************authors8************** {category=fiction, author=Evelyn Waugh, title=Sword of Honour, price=12.99} **************authors9************** {category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99} {category=fiction, author=J. R. R. Tolkien, title=The Lord of the Rings, isbn=0-395-19395-8, price=22.99} **************authors10************** {category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99} {category=fiction, author=J. R. R. Tolkien, title=The Lord of the Rings, isbn=0-395-19395-8, price=22.99} **************authors11************** {category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99} {category=fiction, author=J. R. R. Tolkien, title=The Lord of the Rings, isbn=0-395-19395-8, price=22.99} **************authors12************** {category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95} {category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99} **************authors13************** {category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95} {category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99} **************authors14************** {category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95} **************authors15************** {book=[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}], bicycle={color=red, price=19.95}} 10 [{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}] {color=red, price=19.95} {category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95} {category=fiction, author=Evelyn Waugh, title=Sword of Honour, price=12.99} {category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99} {category=fiction, author=J. R. R. Tolkien, title=The Lord of the Rings, isbn=0-395-19395-8, price=22.99} reference Nigel Rees Sayings of the Century 8.95 fiction Evelyn Waugh Sword of Honour 12.99 fiction Herman Melville Moby Dick 0-553-21311-3 8.99 fiction J. R. R. Tolkien The Lord of the Rings 0-395-19395-8 22.99 red 19.95 **************authors16************** 42.然后看getJsonValue0()、getJsonValue1()、getJsonValue2()、getJsonValue3()、getJsonValue4()的輸出結(jié)果
--------------------------------------getJsonValue0-------------------------------------- Nigel Rees Evelyn Waugh Herman Melville J. R. R. Tolkien --------------------------------------getJsonValue1-------------------------------------- Nigel Rees Evelyn Waugh --------------------------------------getJsonValue2-------------------------------------- Herman Melville J. R. R. Tolkien ****************Map**************** **** category=fiction author=Evelyn Waugh title=Sword of Honour price=12.99 **** category=fiction author=J. R. R. Tolkien title=The Lord of the Rings isbn=0-395-19395-8 price=22.99 --------------------------------------getJsonValue3-------------------------------------- Nigel Rees --------------------------------------getJsonValue4-------------------------------------- ****************books1**************** **** category=fiction author=Herman Melville title=Moby Dick isbn=0-553-21311-3 price=8.99 ****************books2**************** **** category=fiction author=Herman Melville title=Moby Dick isbn=0-553-21311-3 price=8.99?3.相應(yīng)的解釋已經(jīng)在代碼中注釋,更多的用法詳見官網(wǎng)鏈接:https://github.com/jayway/JsonPath
4.如果只是想簡單的處理json,也可以使用JSONObject和JSONArray,具體用法詳見我的這篇文章:http://www.cnblogs.com/qiaoyeye/p/4730930.html
?
----------更新2016年08月19日10:06:09-------
You can use?&&?and?||?to combine multiple predicates?[?(@.price < 10 && @.category == 'fiction')]?,?[?(@.category == 'reference' || @.price > 10)]
?
這個好厲害,可以這樣用
JsonPath.read(json, "$..farePrices[?(@.priceType == 'SalePrice' && @.passengerType == 'ADULT')].amount");
總結(jié)
以上是生活随笔為你收集整理的使用jsonpath从kettle转换json2csv的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Error parsing HTTP r
- 下一篇: arcpy环境搭建