2017-11-21 8 views
0

JSONオブジェクトを更新しようとしていますが、少し複雑です。私のJSONオブジェクトは、次のようになります静的な名前のないJSONObjectの更新

"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オブジェクト。また、.containsの代わりにObjectの値をチェックする別の方法がありますか?

+0

「org.json」を使用して、開始することができます。また、あなたの入力オブジェクトを説明する注意は、配列のように見えますか? – ajc

答えて

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); 
} 
関連する問題