fastjson JSONPath.read()。读取指定字段出错

hwamh0ep  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(118)

String jsonString = "{"code":200,"data":{"list":[{"buyerId":3132211,"buyerName":"","buyerPhoneNo":"","createTime":1552632827696,"headCount":1,"id":639,"itemList":[{"alias":"aaddasss","imageUrl":"1111","itemId":2313,"kdtId":1231,"num":1,"price":300,"sku":"[{"k":"房型","k_id":542,"v":"22222","v_id":11111}]","skuId":10622255,"title":"陈达测试多规格--陈达"}],"kdtId":33333333,"kdtName":"","orderNo":"E2232","orderStatus":100,"orderType":1,"payablePrice":120,"remark":"","seatName":"311111111","serialNo":"1","storeId":0}],"page":1,"pageSize":20,"totalItems":246},"message":"successful","success":true}";
System.out.println(JSONPath.read(jsonString,"$.data.list[0].itemList[0].sku[0].v"));

Exception in thread "main" java.lang.UnsupportedOperationException
at com.alibaba.fastjson.JSONPath.getArrayItem(JSONPath.java:1584)
at com.alibaba.fastjson.JSONPath$ArrayAccessSegement.eval(JSONPath.java:1093)
at com.alibaba.fastjson.JSONPath.eval(JSONPath.java:80)
at com.alibaba.fastjson.JSONPath.read(JSONPath.java:306)

Exception:

pu82cl6c

pu82cl6c1#

你这个应该是不行的吧,sku对应的value是一个字符串,字符串内的内层JSON元素根本就不在外层JSON对象的层次结构中。要达到你的目的,可以这么写:

String jsonString = "{\"code\":200,\"data\":{\"list\":[{\"buyerId\":3132211,"
                + "\"buyerName\":\"\",\"buyerPhoneNo\":\"\",\"createTime\":1552632827696,"
                + "\"headCount\":1,\"id\":639,\"itemList\":[{\"alias\":\"aaddasss\",\"imageUrl\":\"1111\","
                + "\"itemId\":2313,\"kdtId\":1231,\"num\":1,\"price\":300,\"sku\":\"[{\\\"k\\\":\\\"房型\\\","
                + "\\\"k_id\\\":542,\\\"v\\\":\\\"22222\\\",\\\"v_id\\\":11111}]\",\"skuId\":10622255,"
                + "\"title\":\"陈达测试多规格--陈达\"}],"
                + "\"kdtId\":33333333,\"kdtName\":\"\",\"orderNo\":\"E2232\",\"orderStatus\":100,\"orderType\":1,"
                + "\"payablePrice\":120,\"remark\":\"\",\"seatName\":\"311111111\",\"serialNo\":\"1\",\"storeId\":0}],"
                + "\"page\":1,\"pageSize\":20,\"totalItems\":246},\"message\":\"successful\",\"success\":true}";
String jsonString2 = (String)JSONPath.read(jsonString,"$.data.list[0].itemList[0].sku");
System.out.println( JSONPath.read(jsonString2,"[0].v"));

相关问题