在JAVA

时间:2019-04-09 01:13:46

标签: java json

我有这个JSON平面文件,我需要更改clientCode的值

{
  "clientNumber": "Test",
  "clientCode": "12345",
  "clientReference": "JSON Testing",
  "billingCode":"90code",
  "clienttructure": "new, test, Java",
  "site": "sampleStore",
  "siteDestination": "EU",
}

我试图更改clientCode的值并检查方法的返回。

我正在使用Java

String jsonName = "clientCode ";
String jsonValue = "new Value";
String JSONSource = path;


public String put(String jsonName, String jsonValue, String JSONSource) {
        String jsonString = null;

        try {
            Object obj = parser.parse(new FileReader(JSONSource));
            org.json.simple.JSONObject jsonObject = (org.json.simple.JSONObject) obj;

            jsonObject.put(jsonName, jsonValue);

            jsonString = jsonObject.toJSONString();


        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return jsonString;
    }

实际输出为:

{ "billingCode":"90code", "clientCode": "new Value","clientNumber": "Test","clientReference": "JSON Testing",.....}

预期输出应为:

{
  "clientNumber": "Test",
  "clientCode": "new Value",
  "clientReference": "JSON Testing",
  "billingCode":"90code",
  "clienttructure": "new, test, Java",
  "site": "sampleStore",
  "siteDestination": "EU",
}

1 个答案:

答案 0 :(得分:0)

尝试一下...

JSONObject jsonobject =  new JSONObject(response);
jsonobject .remove("clientCode");

JSONObject updateValue = new JSONObject(updateValue);
String UpdatedValue = updateValue.getString("clientCode");
jsonobject .put("clientCode", UpdatedValue);