简体   繁体   English

将新内容添加到JSON文件

[英]Add new content to JSON-File

i have a json-file called "user.json". 我有一个名为“ user.json”的json文件。 Now i want to add a new user. 现在,我想添加一个新用户。 I know how to get the content of the file and put it in a String, but i dont know how to add new content correctly. 我知道如何获取文件的内容并将其放在字符串中,但是我不知道如何正确添加新内容。 "jsonString" is the content of "user.json" “ jsonString”是“ user.json”的内容

    JSONParser parser = new JSONParser();
    Object object = parser.parse(jsonString);
    JSONObject jsonObject = (JSONObject) object;
    jsonObject.put("name", name);
    jsonObject.put("region", region);
    jsonObject.put("v1", v1);
    jsonObject.put("v2", v2);

    System.out.println(jsonObject.toJSONString()); 

The output is: 输出为:

{
    "v1": false,
    "v2": false,
    "name": "test",
    "region": "",
    "user": [
        {
            "v1": "true",
            "v2": "true",
            "name": "UserName",
            "region": ""
        }
    ]
}

But it should be: 但是应该是:

{
    "user": [
        {
            "v1": "true",
            "v2": "true",
            "name": "UserName",
            "region": ""
        },
        {
            "v1": false,
            "v2": false,
            "name": "test",
            "region": ""
        }
    ]
}

Does somebody know how to do it right? 有人知道该怎么做吗? I looked it up but all the examples are not working for me, because when i try 我查了一下,但所有示例都对我不起作用,因为当我尝试时

JSONObject object = new JSONObject(jsonString);

or 要么

JSONArray array = new JSONArray(jsonString);

i always get "the constructor ist undefined". 我总是得到“构造函数未定义”。

Edit: content of user.json 编辑:user.json的内容

{"user":[
{"name":"Testuser1", "region":"A", "v1":"false", "v2":"false"},
{"name":"Testuser2", "region":"B", "v1":"true", "v2":"true"},
{"name":"Testuser3", "region":"B", "v1":"false", "v2":"false"},
{"name":"Testuser4", "region":"A", "v1":"true", "v2":"true"},
{"name":"Testuser5", "region":"A", "v1":"false", "v2":"false"}

]} ]}

Edit2: I solved the problem Edit2:我解决了问题

    Object object = parser.parse(new          FileReader(classLoader.getResource("user.json").getFile()));
    JSONObject jsonObject = (JSONObject) object;
    JSONArray array = (JSONArray) jsonObject.get("user");

    JSONObject newObject = new JSONObject();
    newObject.put("name", name);
    newObject.put("region", region);
    newObject.put("v1", v1);
    newObject.put("v2", v2);
    array.add(newObject);

    Map<String, JSONArray> map = new HashMap<>();
    map.put("\"user\"", array);
    String mapInhalt = map.toString();
    if (mapInhalt.contains("=")) {
        System.out.println("yop");
        mapInhalt.replaceFirst("=", ":");
    }
    System.out.println(mapInhalt);

it seems you are adding fields to your jsonObject , and what you want to do it basically adding the new object to the inner json array (in this case field is called "user" 似乎您正在向jsonObject添加字段,并且您想要执行的操作基本上是将新对象添加到inner json array (在这种情况下,该字段称为"user"

try this: 尝试这个:

JSONObject newObject=new JSONObject();
newObject.put("name", name);
newObject.put("region", region);
newObject.put("v1", v1);
newObject.put("v2", v2);
jsonObject.getJSONArray("user").add(newObject)

you can use fromObject() method: 您可以使用fromObject()方法:

JSONObject json = JSONObject.fromObject(jsonStr);
JSONArray jsonArry = JSONArray.fromObject(jsonStr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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