更新没有静态名称的JSONObject

时间:2017-11-21 11:53:34

标签: java json

我正在尝试更新JSON对象,但我发现它有点复杂。

我的JSON对象如下所示:

  "TableName1.ID": 0,
  "TableName1.value": 0,
  "TableName1.value": 0,
  "TableName2.ID": 0,
  "TableName1.value": 0,
  "TableName1.value": 0,
  "TableName2.value": 0,

会有不同的表格,但有些名称具有相同的名称。

我要做的是将我的JSONObject更新为:

  "TableName1.ID": ChangedValue,
  "TableName1.value": 0,
  "TableName1.value": 0,
  "TableName2.ID": ChangedValue,
  "TableName1.value": 0,
  "TableName1.value": 0,
  "TableName2.value": 0,

我的代码到目前为止:

public static void getJsonValues(JSONArray inputAr) throws JSONException {
for(int i=0;i<inputAr.length();i++) {
    JSONObject jso= inputAr.getJSONObject(i);

    if(jso.toString().contains("ID")) {
        jso.put([This need to be the same as before(e.g. TableName1.Value)],"ChangedValue");
    }
    System.out.println(jso.toString());
}

}

如果我填充jso.put(jso.toString(),"ChangedValue"),它将填充我的数组的每个json对象。还有另一种方法来检查Object值而不是.contains吗?

1 个答案:

答案 0 :(得分:1)

您可以使用Jackson库轻松更改JSON。但肯定还有其他库允许你这样做。

通常我会创建一个包含这些依赖项的Maven项目 - 您也可以使用Gradle

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.8</version>
</dependency>

然后根据这个文件:

{
  "TableName1.ID": 0,
  "TableName1.value1": 0,
  "TableName1.value2": 0,
  "TableName2.ID": 0,
  "TableName2.value1": 0,
  "TableName2.value2": 0,
  "TableName2.value": 0,
  "TableName3.ID": 0,
  "TableName3.value1": 0,
  "TableName3.value2": 0,
  "TableName3.value": 0
}

您可以将ID替换为例如:

{
  "TableName1.ID" : "ffa7aa01-e399-4acc-bd8d-d078b327ec49",
  "TableName1.value1" : 0,
  "TableName1.value2" : 0,
  "TableName2.ID" : "4e416251-804d-4b2c-bdb3-a2ca7e7366ef",
  "TableName2.value1" : 0,
  "TableName2.value2" : 0,
  "TableName2.value" : 0,
  "TableName3.ID" : "1cf4900d-f5e6-4abe-810d-336e45313f62",
  "TableName3.value1" : 0,
  "TableName3.value2" : 0,
  "TableName3.value" : 0
}

在这里使用此代码(请参阅评论,了解它的作用):

// read the file and make sure the input stream is closed after leaving the block.
try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("so_example.json")) {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readTree(in); // create a tree structure from the JSON
    jsonNode.fields().forEachRemaining(entry -> { // loop through the JSON fields and change only the values of the elements with a certain pattern
        if(entry.getKey().endsWith(".ID")) {
            entry.setValue(new TextNode(UUID.randomUUID().toString()));
        }
    });
    String res = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode); // convert the in memory structure to a pretty string
    System.out.println(res);
}