Sanzhar
Sanzhar

Reputation: 131

convert JSON Object to List<String>

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 .

Upvotes: 0

Views: 4793

Answers (3)

jhhwilliams
jhhwilliams

Reputation: 2582

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);

Upvotes: 0

Chirag
Chirag

Reputation: 56925

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

Upvotes: 1

O_o
O_o

Reputation: 1103

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!

Upvotes: 1

Related Questions