在Java中,如何将JSONObject写入包含JSONArray的文件?

在Java中,如何将JSONObject写入包含JSONArray的文件?,java,json,jsonobject,json-simple,Java,Json,Jsonobject,Json Simple,我有一个JSON文件,需要再次读取、编辑和写入 阅读效果很好,我很难处理数据中JSON数组的写入部分 我使用库在Java中处理JSON 该文件如下所示: { "maxUsers":100, "maxTextLength":2000, "maxFileSize":2000, "services": [ { "serviceName":"Яндекc", "className":"YandexCo

我有一个JSON文件,需要再次读取、编辑和写入

阅读效果很好,我很难处理数据中JSON数组的写入部分

我使用库在Java中处理JSON

该文件如下所示:

{
    "maxUsers":100,
    "maxTextLength":2000,
    "maxFileSize":2000,
    "services":
    [
        {
            "serviceName":"Яндекc",
            "className":"YandexConnector.class",
            "isEnabled":true
        },

        {
            "serviceName":"Google",
            "className":"GoogleConnector.class",
            "isEnabled":false
        }
    ]
}
        JSONArray servicesJSON = new JSONArray();
        ArrayList<Service> servicesArray = this.getServices();
        for(int i=0; i< servicesArray.size(); i++)
        {
            servicesJSON.add(servicesArray.get(i).toMap()); // use the toMap method here.
        }
        obj.put("services", servicesJSON);
当我尝试将JSON数据(变量
obj
)写入文件时,
services
数组被破坏。我的写作代码:

JSONObject obj = new JSONObject();
obj.put("maxUsers", this.getMaxUsers());
obj.put("maxTextLength", this.getMaxTextLength());
obj.put("maxFileSize", this.getMaxFileSize()); 

JSONArray servicesJSON = new JSONArray();
ArrayList<Service> servicesArray = this.getServices();
for(int i=0; i< servicesArray.size(); i++)
{
    servicesJSON.add(servicesArray.get(i));
}
obj.put("services", servicesJSON);


FileWriter file = new FileWriter(filename);                
obj.writeJSONString(file);
file.flush();
file.close();
如果像
services
这样的JSONArray中有JSON数据,如何将其正确写入文件


代码,其中我从文件中读取JSON数据(工作正常):

JSONParser=newjsonparser();
objectobj=parser.parse(新文件读取器(文件名));
JSONObject JSONObject=(JSONObject)对象;
setMaxUsers((Long)jsonObject.get(“maxUsers”);
setMaxTextLength((Long)jsonObject.get(“maxTextLength”);
setMaxFileSize((长)jsonObject.get(“maxFileSize”);
//获取所有服务列表
JSONArray serv=(JSONArray)jsonObject.get(“服务”);
对于(int i=0;i
编辑部分尚未编写,因此我在阅读之后直接调用编写部分


看一看
JSON simple
的用法

这里说您需要使用
原语
字符串
值将对象逐个放入数组中。您可以使用
集合
映射
,它们本身只包含
字符串
原语

  JSONArray list = new JSONArray();
  list.add("foo");
  list.add(new Integer(100));
  list.add(new Double(1000.21));
  list.add(new Boolean(true));
  list.add(null);
  StringWriter out = new StringWriter();
  list.writeJSONString(out);
因此,添加您的
服务是不允许的,也不会起作用。您应该在其中添加一个
toMap
方法,将其转换为
Map
fromMap
以将其转换回来

像这样(在
Services.java
中):

看看JSONArray。在这里您将得到可用方法的列表。这个
JSONArray
继承自
java.util.ArrayList
,因此我们可以使用
ArrayList
的可用方法来
JSONArray

 JSONArray userList = JSONFile.readJSONArray("users.json");
 JSONArray newuserList = new JSONArray() ;  
 JSONObject jsonobject , newjsonObject;
            for (int i = 0; i < userList.size(); i++) {
                jsonobject = (JSONObject) userList.get(i);

                String id = (String) jsonObject.get("id");
                String pass = (String) jsonObject.get("password");
                newuserList.add(jsonObject); 
 // Here we are putting json object into newuserList which is of JSONArray type
            try{
                FileWriter  file = new FileWriter( "/users.json",false);

                newuserList.writeJSONString(newuserList, file);
                file.close();
            }
            catch(Exception e  ){

                e.getMessage();

            }
JSONArray userList=JSONFile.readJSONArray(“users.json”);
JSONArray newuserList=newjsonarray();
JSONObject JSONObject、newjsonObject;
对于(int i=0;i

希望这会有所帮助!

编辑部分肯定有问题,您没有显示。您将服务类实例存储在JSON数组中,而不是存储JSONObject实例。向我们展示如何创建JSON数据
obj
@SotiriosDelimanolis,我用代码添加部分。您能看一下吗?@jbniset-我用c添加部分ode是我处理对象JSON的地方。你能看一下吗?别忘了展示如何将其序列化回文件。在将JSON写入文件时,它应该调用JSONArray的静态方法:
JSONArray.writeJSONString(newuserList,file);
在当前版本的JSON simple中。
 public Map toMap() {
     HashMap<String, String> serviceAsMap = new HashMap<>();
     servicesAsMap.put("serviceName", serviceName);
     servicesAsMap.put("className", this.class.getName() + ".class");
     servicesAsMap.put("isEnabled", isEnabled);
     // ... continue for all values
     return servicesAsMap;
 }
        JSONArray servicesJSON = new JSONArray();
        ArrayList<Service> servicesArray = this.getServices();
        for(int i=0; i< servicesArray.size(); i++)
        {
            servicesJSON.add(servicesArray.get(i).toMap()); // use the toMap method here.
        }
        obj.put("services", servicesJSON);
 JSONArray userList = JSONFile.readJSONArray("users.json");
 JSONArray newuserList = new JSONArray() ;  
 JSONObject jsonobject , newjsonObject;
            for (int i = 0; i < userList.size(); i++) {
                jsonobject = (JSONObject) userList.get(i);

                String id = (String) jsonObject.get("id");
                String pass = (String) jsonObject.get("password");
                newuserList.add(jsonObject); 
 // Here we are putting json object into newuserList which is of JSONArray type
            try{
                FileWriter  file = new FileWriter( "/users.json",false);

                newuserList.writeJSONString(newuserList, file);
                file.close();
            }
            catch(Exception e  ){

                e.getMessage();

            }