0

in the past i have already parsed a json file from url to local sqlite database and the json source was like the following one:

[
    {
        "origin": "accommodation",
        "parent": "",
        "en": "Accommodation"
    },
    {
        "origine": "book",
        "parent": "shopping",
        "en": "Book"
    },
    {
        "origin": "university",
        "parent": "education",
        "en": "University"
    }
]

But now it seems to be different. I validate the following json file so i'm sure it is a valid json, but it is formatted in a different way, so i do not know how to parse it in the right way. In addition this time i would put the content in an ArrayList. Here is the json file:

{
    "city1": {
        "item1": {
            "score": 6196.58,
            "step": 0.00423,
            "name": "User 1",
            "form": "yellow"
        },
        "item2": {
            "score": 106.86,
            "step": 2.5822,
            "name": "User 2",
            "form": "yellow"
        },
        "item3": {
            "score": 671.48,
            "step": 0.387,
            "name": "User 3",
            "form": "yellow"
        },
    },
    "misc": {
        "online_users": {
            "kind1": 18,
            "kind2": 3
        },
        "ops": [
            ""
        ]
    },
    "city2": {
        "item1": {
            "score": 6196.58,
            "step": 0.00405,
            "name": "User 1",
            "form": "yellow"
        },
        "item2": {
            "score": 179563.93,
            "step": 0.000138,
            "name": "User 2",
            "form": "yellow"
        },
        "min_size": {
            "line": 10
        },
        "out_of_sync": [
            "0e888069530497506433b5f0cacb",
            "b428fa3a9b9e13cf8b26196bfcc7",
            "f42442a2e46523f059809f83f1a9"
        ],
    },
}

Can you tell me how to handle these values and how to put in some arraylist?

smartmouse
  • 13,912
  • 34
  • 100
  • 166
  • 1
    In the first json example you had array of objects but now you have mostly json objects nested. I think this might help you http://stackoverflow.com/questions/5015844/parsing-json-object-in-java – Prudhvi Feb 22 '15 at 22:02
  • How many "itemX" can be there in a "city" object? – ChaturaM Feb 23 '15 at 02:18

2 Answers2

2

There are several ways to do this, but I'll post the way I feel like makes the most sense. If you don't mind putting this in separate arrays you can do this:

String stringOfJSONCode = <read in the JSON here>;
JSONObject json = new JSONObject(stringOfJSONCode);
Iterator<String> iter = json.keys();
while (iter.hasNext()) {
    String key = iter.next();
    try {
        Object value = json.get(key);
    } catch (Exception e) {
    }
}

You need to use an iterator to loop through a JSONObject. You could use any other loop to loop through JSONArrays. This will iterate through the outer objects, like city1, misc, city2. The key will be either city1, misc, or city2. If you want to get those, just add this within the 'try'

JSONObject object = json.getJSONObject(key);
             or
JSONArray array = json.getJSONArray(key);

depending on what you're trying to get. Once you have these, you can do the following:

JSONObject city1 = object.getJSONObject("item1");
List<String> jsonList = new ArrayList<String>();
jsonList.add(object.getDouble("score"));
jsonList.add(object.getDouble("step"));
jsonList.add(object.getString("name"));
jsonList.add(object.getString("form"));

If you are not sure if it will be item1 or whatever, you can iterate through it again using the iterator and add each item in dynamically. Also you can make another try-catch, try to get a JSONArray, if it is not an array it will go in the catch, and then iterate through the object.

You could also make a hashmap or 2D array to add everything in the same array.

Hope this helps

Kenneth Streit
  • 1,042
  • 9
  • 12
  • Sorry, i'm afraid i do not understand your code. For example if i need just to get item3 of city1 and item2 of city2, what is the right code? – smartmouse Feb 22 '15 at 22:42
  • 1
    This is just iterating through your JSON string. It is like a for-loop; key is going to get each outer JSON object, which in your case would be city1, misc, and city2. So you can do a check: `if (key.equals("city1")) JSONObject item3 = json.getJSONObject("item3");` or you can do `if (key.equals("city2)) JSONObject item2 = json.getJSONObject("item2");` Then, if you choose to get item3 of city1, and you want to get the score, just do `double score = item3.getDouble("score");` or you can get name by saying `String name = item3.getString("name");` – Kenneth Streit Feb 23 '15 at 00:15
0

Why don't you let jackson mapper do it's magic?

The only thing you'll need then would be appropriate DTO classes.

simonides
  • 3,040
  • 3
  • 19
  • 34