Java-编辑现有的JSON文件 [英] Java - Edit existing JSON file

查看:49
本文介绍了Java-编辑现有的JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想知道是否可以编辑一个我知道该位置的json.所以可以说位置是C:\ Something.json那我该如何编辑???

Hey I was wondering if it is possible to edit an existing json I know the location off. So lets say that the location is C:\Something.json then how do I edit it ??.

谢谢

推荐答案

如果您要编辑复杂的JSON结构,建议使用

If you want to edit complex JSON structure I would recommend using GSON library. You can then perform the following steps :

Gson gson = new Gson();
String yourfilecontents; //read contents from File

1)读取JSON文件并将其转换为对象结构.

1) read JSON File and convert it into an object structure.

 YourObject obj = gson.fromJson(yourfilecontents, YourObject.class);

2)根据您的要求修改对象.

2) Modify the object as per your requirement.

obj.field1(newValue);

3)将对象转换为JSON

3) Convert the object into JSON

String newJson = gson.toJson(obj);

4)将JSON(newJson)写回到文件中

4) Write the JSON (newJson) back to the file

这篇关于Java-编辑现有的JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆