Search code examples
c#jsonwindows-phone-8

Parsing a multidimensional JsonArray dynamically


I'm trying to parse json data from the api. The json data I'm trying to parse can be seen here at enter image description here

i want to show the output in listbox for as below enter image description here

here is what im trying to do

dynamic json = JsonConvert.DeserializeObject(jsons);

foreach (var abc in json.sfkpi)
{
    Resultclass objs = new Resultclass();

    string months = abc.month;
    if (months == "Jan")
    {

        objs.p1 = (string)json["sfkpi"][0]["optima"][0]["productValue"];
        objs.p2 = (string)json["sfkpi"][0]["optima"][0]["productValue"];
        objs.p3 = (string)json["sfkpi"][0]["optima"][0]["productValue"];

        lstAnnouncement.Items.Add(objs);
    }
}

but here it adds the same value for for every month that is first productValue so what i am doing wrong how to achieve it.


Solution

  • You have 3 elements in sfkpi, and you are accessing only one element

    //look at the index 0
    (string)json["sfkpi"][0]
    (string)json["sfkpi"][0] 
    (string)json["sfkpi"][0]
    

    You just have to change the index

    (string)json["sfkpi"][0]
    (string)json["sfkpi"][1] 
    (string)json["sfkpi"][2]