dentex
dentex

Reputation: 3263

append data to existing JSON file

I'm using this code:

JSONObject jO = new JSONObject();

try {
    jO.put("item1", true);
    jO.put("item2", value2);
    jO.put("item3", value3);
} catch (JSONException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

String json = null;
try {
    json = jO.toString(4);
} catch (JSONException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

File jsonFile = new File(nContext.getDir("json", 0), "dashboard.json");
//simple utility method to write the json file
Utils.writeToFile(jsonFile, json);

to have this result:

{
    "item3": "12345",
    "item2": "abcde",
    "item1": true
}

What I want to achieve, on the next run of the same piece of code, is to end with something like:

{
    "pass1": {
        "item3": "12345",
        "item2": "abcde",
        "item1": true
    },
    "pass2": {
        "item3": "67890",
        "item2": "zxcvb",
        "item1": true
    }
}

Or maybe is it better to have this?

{
    "pass1": [
        {
            "item3": "12345",
            "item2": "abcde",
            "item1": true
        }
    ],
    "pass2": [
        {
            "item3": "67890",
            "item2": "zxcvb",
            "item1": true
        }
    ]
}

I know this implies a change in the code to include a "nested" object/array. Which one is better, considering that I'll have to parse the JSON to build a ListView? Any ideas?

Upvotes: 1

Views: 7984

Answers (2)

dentex
dentex

Reputation: 3263

I found the solution, thanks to the comments by other users and to a "retired" answer, not present here anymore. Maybe it was my fault not being clear.

public void addEntryToJsonFile(Context ctx, String id, String name, String size) {

    // parse existing/init new JSON 
    File jsonFile = new File(ctx.getDir("my_data_dir", 0), "my_json_file.json");
    String previousJson = null;
    if (jsonFile.exists()) {
        try {
            previousJson = Utils.readFromFile(jsonFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        previousJson = "{}";
    }

    // create new "complex" object
    JSONObject mO = null;
    JSONObject jO = new JSONObject();

    try {
        mO = new JSONObject(previousJson);
        jO.put("completed", true);
        jO.put("name", name);
        jO.put("size", size);
        mO.put(id, jO); //thanks "retired" answer
    } catch (JSONException e) {
        e.printStackTrace();
    }

    // generate string from the object
    String jsonString = null;
    try {
        jsonString = mO.toString(4);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    // write back JSON file
    Utils.writeToFile(jsonFile, jsonString);

}

Upvotes: 2

njzk2
njzk2

Reputation: 39406

Edited after dentex comment

  1. Read your file
  2. Parse the root Json object
  3. If the root object not is already a complex object
    1. Create a new root object
    2. put your root object in it
  4. put your second object in the root object
  5. Write bnack your file

in pseudo code:

oldJson = ParseJsonFromFile()
newJson = {"item1": true, "item2": "abcde" ...}
JSONObject root;
if (oldJson.hasKey("pass1") {
    root = oldJson
} else {
    root = new JSONObject()
    root.add("pass1", oldJson)
}
root.add("pass" + root.getSize() + 2, newJson)
WriteJsonToFile(root)

Upvotes: 1

Related Questions