2017-11-21 1 views
0

JSON 개체를 업데이트하려고하지만 다소 복잡합니다.정적 이름없이 JSONObject 업데이트

내 JSON 개체는 다음과 같습니다

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

다른 테이블이있을 것입니다하지만 같은 이름을 가진 일부 값이 있습니다. 내가 jso.put(jso.toString(),"ChangedValue")을 채울 경우

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()); 
} 

}

는 모든 채우기 : 지금까지

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

내 코드 :

내가 뭘하려고 업데이트는 다음과 같이 내 된 JSONObject입니다 내 배열의 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); 
}