我有一个JSON文件,即test.json
。
{
"Added": {
"type": "K",
"newmem": {
"IDNew": {
"id": "777709",
"type": "LOP"
},
"birthDate": "2000-12-09"
},
"code": "",
"newest": {
"curlNew": "",
"addedForNew": ""
}
}
}
我尝试了以下代码:
File file = new File("test.json");
JSONParser parser = new JSONParser();
JSONObject data = (JSONObject) parser.parse(
new FileReader(file.getAbsolutePath()
));//path to the JSON file.
System.out.println(data.toString());
JSONObject jObject = new JSONObject();
jObject.put("id","12345678");
System.out.println(jObject);
获得结果:-
{
"Added": {
"type": "K",
"newmem": {
"IDNew": {
"id": "777709",
"type": "LOP"
},
"birthDate": "2000-12-09"
},
"code": "",
"newest": {
"curlNew": "",
"addedForNew": ""
}
}
}{
"id":"12345678"
}
值id
:“ 777709”未更新为id
:“ 12345678”,但最后添加了。请帮助我并告诉我如何替换id
值。
答案 0 :(得分:4)
您可以按照以下方式使用simple-json java lib更新JSONObject
中的嵌套元素:
JSONObject added = (JSONObject) data.get("Added");
JSONObject newman = (JSONObject) added.get("newmem");
JSONObject idNew = (JSONObject) newman.get("IDNew");
idNew.put("id","12345678");
System.out.println(data);
答案 1 :(得分:2)
您可以使用简单的json库(library)进行尝试。我分别打印了所有对象以供理解。因为您在另外两个对象中声明了Id对象,所以首先您必须获取此对象,然后获取所需的对象IDNew。然后在id字段中输入新的id值。
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Main {
private static final String filePath = "E:\\project-test\\scloud\\test\\src\\main\\resources\\test";
public static void main(String[] args) {
try {
// read the json file
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
System.out.println(jsonObject);
JSONObject addedObj = (JSONObject) jsonObject.get("Added");
System.out.println("Added is: " + addedObj);
JSONObject newmemObject =(JSONObject) addedObj.get("newmem");
System.out.println("newmemObject is: " + newmemObject);
JSONObject idNewObj =(JSONObject) newmemObject.get("IDNew");
System.out.println("IdNewObj is: " + idNewObj);
long id =Long.valueOf((String) idNewObj.get("id"));
System.out.println(id);
idNewObj.put("id",809809809);
System.out.println(jsonObject);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ParseException ex) {
ex.printStackTrace();
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}
}
或者为简单起见,您可以使用此
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
System.out.println(jsonObject);
JSONObject idObj = (
(JSONObject)
(
(
JSONObject)
(
(JSONObject)
jsonObject.get("Added")).
get("newmem"))
.get("IDNew"));
idObj.put("id", 98009809);
System.out.println("After ID value updated : "+jsonObject);
答案 2 :(得分:1)
使用不同的库json-path的另一种解决方案:
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat;
@Test
public void exampleToReplaceSingleElement_jsonTakenFromFile() throws IOException {
String expectedId = "12345678";
String expectedJson = "{\n" +
" \"Added\": {\n" +
" \"type\": \"K\",\n" +
" \"newmem\": {\n" +
" \"IDNew\": {\n" +
" \"id\": \"12345678\",\n" +
" \"type\": \"LOP\"\n" +
" },\n" +
" \"birthDate\": \"2000-12-09\"\n" +
" },\n" +
" \"code\": \"\",\n" +
" \"newest\": {\n" +
" \"curlNew\": \"\",\n" +
" \"addedForNew\": \"\"\n" +
" }\n" +
" }\n" +
"}";
Configuration configuration = Configuration
.builder()
.options(Option.SUPPRESS_EXCEPTIONS)
.build();
File json = new File("src/test/resources/test.json");
System.out.println(json.getAbsolutePath());
DocumentContext parsed = JsonPath.using(configuration).parse(json);
parsed.set("$.Added.newmem.IDNew.id", expectedId);
String actual = parsed.jsonString();
log.info("After ID value updated: {}", actual);
assertThat(actual).isEqualToIgnoringWhitespace(expectedJson);
}
也可以在test exampleToReplaceSingleElement()或test exampleToReplaceSingleElement_jsonTakenFromFile()上访问示例。