简体   繁体   中英

Read From JSON File and after adding a property to that object write back to it

I have an empty json file in my java project with only json object bracket:- that is

a.json

{}

i want to read from it and after adding a key value write back to it, that is:-

{"a":"a"}

Is there any Json Library i can use to do so.

I don't wanna use simple FileReaders and FileWriters in java to read and write.

There are several libraries for this task.

One could be Jettison https://mvnrepository.com/artifact/org.codehaus.jettison/jettison

Path filePath = <your-file-path>;
String json = new String(Files.readAllBytes(filePath));
JSONObject jsonObject = new JSONObject(json);
jsonObject.put("a", "a");
Files.write(filePath, jsonObject.toString().getBytes());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM