0

I have following JSON Object

 {"LIST":{"row_12":{"id":"11","name":"blah blah"},
"row_13":{"id":"8","name":"blah blah"},
"row_10":{"id":"3","name":"blah blah"},
"row_11":{"id":"14","name":"blah blah"},
"row_2":{"id":"27","name":"blah blah"},
"row_1":{"id":"2","name":"blah blah"},
"row_4":{"id":"23","name":"blah blah"},
"row_14":{"id":"43","name":"blah blah"},
"row_15":{"id":"35","name":"blah blah"},
"row_3":{"id":"36","name":"blah blah"},
"row_5":{"id":"30","name":"blah blah"},
"row_6":{"id":"34","name":"blah blah"},
"row_7":{"id":"17","name":"blah blah"},
"row_8":{"id":"37","name":"blah blah"},
"row_9":{"id":"1","name":"blah blah"}}} 

I want to convert it to List I tried several codes from the answers to questions which are similar to mine, however they didn`t work, I think it is because each object in row has its name. How can I convert the object to List .

Sanzhar
  • 131
  • 2
  • 14
  • Show what have you tried. Also refer to http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Nabin Nov 14 '16 at 10:29
  • 1
    basically what values do you need in your List ?Whether you need id and name or only names in list – Shadow Droid Nov 14 '16 at 10:39

3 Answers3

1
JSONObject list = response.getJSONObject("LIST");
JSONObject rowOne = list.getJSONObject("row_12");
String id = rowOne.getString("id");
String name = rowOne.getString("name");

You follow this procedure 14 times, since your responses key,value pair is different. You should ask kor an JSONArray inside list for each row. "response" is the JSONObject you receive on your on Async recieve method. Hope it helps. [PS: your question is a bad practice in stackOverFlow. Please check this link to see stackOverFlow's question posting method.] Cheers!

Community
  • 1
  • 1
O_o
  • 1,103
  • 11
  • 36
  • thanks mate, so there is no way to put all those objects to one JSON Array? – Sanzhar Nov 14 '16 at 10:35
  • Anytime mate.. It's the server end's job to send you a pretty JSONResponse. All you should do is parse it. Server sends you a pretty amateur json response. Instead of using for loop, s/he wrote 15 lines of codes probably to send you the response. Cheers! – O_o Nov 14 '16 at 10:38
  • @Sanzhar, other answer will enable you to parse without any change from server response. I don't think it's a good practice, especially if you want to do one thing based on one value, but it will work for your current response. – O_o Nov 14 '16 at 10:40
  • Understood, yeah I thought that people might find my question inappropriate thanks once again – Sanzhar Nov 14 '16 at 10:43
  • @Sanzhar, it is kind of, since there are lots of same question for JSON Response. If you are finished with this question, please up-vote both answer, instead of accepting one, since both of them are correct. :) – O_o Nov 14 '16 at 10:45
1

You have to use keys() iterator to get all the properties and then call for get() on each object.

Iterator<String> iter = json.keys();
while (iter.hasNext()) {
    String key = iter.next();
    try {
        Object value = json.get(key);
    } catch (JSONException e) {
        // went wrong!
    }
}

// here json is the object of "LIST" Json Object.

Here is the link for your reference. Iterating through JSONObject

Community
  • 1
  • 1
Chirag
  • 56,621
  • 29
  • 151
  • 198
0

I came across this article by Thiago Passos which helped me solve this problem in C#

Given the following JSON:

{
    "Bar1": {
        "Baz": "Baz1"
    },
    "Bar2": {
        "Baz": "Baz2"
    },
    "Bar3": {
        "Baz": "Baz3"
    },
    "Bar4": {
        "Baz": "Baz3"
    }
}

Normally, this would parse to the following classes:

public class Foo
{
    public Bar1 Bar1 { get; set; }
    public Bar2 Bar2 { get; set; }
    public Bar3 Bar3 { get; set; }
    public Bar4 Bar4 { get; set; }
}

public class Bar1 //Bar2, Bar3 & Bar4
{
    public string Baz { get; set; }
}

What we want instead, is to parse to the following class:

public class Foo
{
    public List<Bar> Bars { get; set; }
}

To achieve this, the following JSON converter can be used:

public class BarConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteStartArray();
        foreach (var bar in (List<Bar>) value)
        {
            writer.WriteRawValue(JsonConvert.SerializeObject(bar));
        }
        writer.WriteEndArray();
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var response = new List<Bar>();
        JObject bars = JObject.Load(reader);
        foreach (var bar in bars)
        {
            var b = JsonConvert.DeserializeObject<Bar>(bar.Value.ToString());
            //b.Id = bar.Key;
            response.Add(b);
        }

        return response;
    }

    public override bool CanConvert(Type objectType) => objectType == typeof(List<Bar>);
}

Update the Foo class to use the converter:

public class Foo
{
    [JsonConverter(typeof(BarConverter))]
    public List<Bar> Bars { get; set; }
}

Finally, deserialize the JSON as usual:

var foo = JsonConvert.DeserializeObject<Foo>(input);
jhhwilliams
  • 2,297
  • 1
  • 22
  • 26