简体   繁体   中英

Add new content to JSON-File

i have a json-file called "user.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"

    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":[
{"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

    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"

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:

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

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