Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么可以';我是否使用Jayway JsonPath设置json属性的值?_Java_Json_Jsonpath - Fatal编程技术网

Java 为什么可以';我是否使用Jayway JsonPath设置json属性的值?

Java 为什么可以';我是否使用Jayway JsonPath设置json属性的值?,java,json,jsonpath,Java,Json,Jsonpath,我需要更新JSON中的值。我需要更新的密钥路径也基于某些条件,因此我必须在属性文件中维护路径。 这是我的JSON String jsonString = "{\"delivery_codes\": [{\"postal_code\": {\"district\": \"District1\", \"pin\": 201001, \"pre_paid\": \"Y\", \"cash\": \"Y\", \"pickup\": \"Y\", \"repl\": \"N\", \"cod\": \"

我需要更新JSON中的值。我需要更新的密钥路径也基于某些条件,因此我必须在属性文件中维护路径。 这是我的JSON

String jsonString = "{\"delivery_codes\": [{\"postal_code\": {\"district\": \"District1\", \"pin\": 201001, \"pre_paid\": \"Y\", \"cash\": \"Y\", \"pickup\": \"Y\", \"repl\": \"N\", \"cod\": \"Y\", \"is_oda\": \"N\", \"sort_code\": \"GB\", \"state_code\": \"UP\"}}]}";
现在我的要求是假设我需要将预付款的值更新为N,但需要预付款的路径是动态的,因为有时我可能需要更新状态代码、排序代码或预付款等。 所以我使用了JSONPath:

String jsonPathExpressionForDistrict = "$['delivery_codes'][0]['postal_code']['district']";
String jsonPathExpressionForState_code = "$['delivery_codes'][0]['postal_code']['state_code']";
我需要将这些路径存储在一些属性文件/DB中

我可以通过以下代码读取该物业:

JsonPath.parse(jsonString).read(jsonPathExpressionForDistrict , JsonNode.class) -> This gives me value 'District1'
JsonPath.parse(jsonString).put(jsonPathExpressionForDistrict , "District2", String.class);
但当我尝试使用以下代码更新值时:

JsonPath.parse(jsonString).read(jsonPathExpressionForDistrict , JsonNode.class) -> This gives me value 'District1'
JsonPath.parse(jsonString).put(jsonPathExpressionForDistrict , "District2", String.class);
这给了我以下错误:

线程“main”中出现异常 com.jayway.jsonpath.InvalidModificationException:只能添加 映射的属性 在com.jayway.jsonpath.internal.PathRef$ObjectPropertyPathRef.put(PathRef.java:265)上 位于com.jayway.jsonpath.jsonpath.put(jsonpath.java:304) 位于com.jayway.jsonpath.internal.JsonContext.put(JsonContext.java:221) 位于com.jayway.jsonpath.internal.JsonContext.put(JsonContext.java:199) 在com..service.ServiceImpl.main(ServiceImpl.java:179)


请有人指导我。

使用org.json库

您可以获取JSONArray并获取数组中项目的键

您可以获得如下项目:

array.getJSONObject(i).getInt("postal_code");
在本例中,i是JSONArray中的项


祝你好运

使用org.json库

您可以获取JSONArray并获取数组中项目的键

您可以获得如下项目:

array.getJSONObject(i).getInt("postal_code");
在本例中,i是JSONArray中的项


祝你好运

当需要更新现有节点的值时,应使用set方法。我希望下面剪掉的部分有帮助:

String jsonString = "{\"delivery_codes\": [{\"postal_code\": {\"district\": \"District1\", \"pin\": 201001, \"pre_paid\": \"Y\", \"cash\": \"Y\", \"pickup\": \"Y\", \"repl\": \"N\", \"cod\": \"Y\", \"is_oda\": \"N\", \"sort_code\": \"GB\", \"state_code\": \"UP\"}}]}";

    String jsonPathExpressionForDistrict = "$['delivery_codes'][0]['postal_code']['district']";
    String jsonPathExpressionForState_code = "$['delivery_codes'][0]['postal_code']['state_code']";

    DocumentContext documentContext = JsonPath.parse(jsonString);
    System.out.println(documentContext.jsonString());

    String read = documentContext.read(jsonPathExpressionForDistrict);

    System.out.println(read);
    documentContext.set(jsonPathExpressionForState_code, "District2");

    System.out.println(documentContext.jsonString());

当需要更新现有节点的值时,应使用set方法。我希望下面剪掉的部分有帮助:

String jsonString = "{\"delivery_codes\": [{\"postal_code\": {\"district\": \"District1\", \"pin\": 201001, \"pre_paid\": \"Y\", \"cash\": \"Y\", \"pickup\": \"Y\", \"repl\": \"N\", \"cod\": \"Y\", \"is_oda\": \"N\", \"sort_code\": \"GB\", \"state_code\": \"UP\"}}]}";

    String jsonPathExpressionForDistrict = "$['delivery_codes'][0]['postal_code']['district']";
    String jsonPathExpressionForState_code = "$['delivery_codes'][0]['postal_code']['state_code']";

    DocumentContext documentContext = JsonPath.parse(jsonString);
    System.out.println(documentContext.jsonString());

    String read = documentContext.read(jsonPathExpressionForDistrict);

    System.out.println(read);
    documentContext.set(jsonPathExpressionForState_code, "District2");

    System.out.println(documentContext.jsonString());

我知道已经很晚了,但也许这可以帮助其他人。 OP得到该错误是因为value
jsonPathExpressionForDistrict
是指向属性的路径,而不是JSON对象或映射

要将
地区2
添加到
邮政编码
,路径应为
“$['delivery\u codes'][0]['postal\u code']”


要使用JSonPath更新/添加JSON中的任何值,我们可以使用文章中提到的方法,但是需要检查并确保该路径在源JSON对象中有效。

我知道已经晚了,但这可能会帮助其他人。 OP得到该错误是因为value
jsonPathExpressionForDistrict
是指向属性的路径,而不是JSON对象或映射

要将
地区2
添加到
邮政编码
,路径应为
“$['delivery\u codes'][0]['postal\u code']”


要使用JSonPath更新/添加JSON中的任何值,我们可以使用本文中提到的方法,但是,需要检查并确保路径在源JSON对象中有效。

我的要求是如何动态给出密钥的路径。我的要求是如何动态给出密钥的路径。可能格式化JSON使其可读?可能格式化JSON使其可读?