0

I need to parse a JSON object that looks like the following (It has no name for the array):

{
"id": "123123",
[{
"id": "456456",
"name": "name1",
"content": "content1"
}, {
"id": "789789",
"name": "name2",
"content": "content2"
}]
}

I have two objects that look like this:

public class MyFirstObject {
    private String id;
    private List<MySecondObject> lst_entries;
}

public class MySecondObject {
    private String id;
    private String name;
    private String content;
}

How do I parse the List of MySecondObject if the original JSON has no name for that object?

I've been looking for something like this in stackoverflow and haven't been able to find a answer or a case similar as mine that could help...

I'll appreciate help. Thanks!

Swinds
  • 26
  • 1
  • 6

2 Answers2

0

The first thing is your JSON format is wrong. I guess you mistyped it. So what you have is.

{
"id": "123123",
[{
"id": "456456",
"name": "name1",
"content": "content1"
}, {
"id": "789789",
"name": "name2",
"content": "content2"
}]
}

The second item in the above JSON that is an array is missing its name. And you cannot put item in a json object without a name. It should always be in name and value pair. So if you want to make the above JSON valid it would be something like this.

{
    "id": "123123",
    "users": [{
        "id": "456456",
        "name": "name1",
        "content": "content1"
    }, {
        "id": "789789",
        "name": "name2",
        "content": "content2"
    }]
} 

This is a valid JSON. And now you can easily parse it.

try {
        JSONObject obj = new JSONObject(jsonString);

    //getting the id
    String id = obj.getString("id");

    //getting the users
    JSONArray users = obj.getJSONArray("users");

    //users is a JSON array so you need to loop through it
    for(int i = 0; i<users.length(); i++){

        //getting every user from the array 
        JSONObject objUser = users.getJSONObject(i);

        //reading user values 
        String uid = objUser.getString("id");
        String name = objUser.getString("name");
        String content  = objUser.getString("content");

    }

} catch (JSONException e) {
    e.printStackTrace();
}

Thank You :) And if you want to learn about JSON very quickly you can check my 7 minute JSON video on YouTube. Learn JSON in 7 Minutes.

Belal Khan
  • 2,099
  • 2
  • 22
  • 32
  • I know that it may be malformed, it is said on the title, but I want to know if there is a possible way of doing it because they send it to me like this and they want me to parse it... – Swinds Jun 12 '19 at 09:21
  • You can parse it but not as json. Actually you can get the string from the first square bracket till the last square bracket with the help of string functions and then you have a correct json array that can be easily parsed. – Belal Khan Jun 12 '19 at 11:20
0

If you insist to parse the invalid JSON, could be done by brute force. Use regex to separate id string and array string, use them to construct valid JSON then convert to object.
However, such approach is very improper. Better just ask the provider to modify it.

TylerQITX
  • 318
  • 2
  • 9
  • 1
    Better solution is to ask the provider to modify it, always try to avoid brute force. Thanks for the help! – Swinds Jun 12 '19 at 09:56