Home > Enterprise >  How to get multiple values from array in JObject with JToken and store them in list
How to get multiple values from array in JObject with JToken and store them in list

Time:07-08

I have following response message from server as string:

string receivedResult = "{\"method\":\"_result\",\"params\":[[{\"idmake\":1,\"name\":\"Abarth\",\"important\":0,\"description\":{\"DE\":\"Abarth & C. S.p.A., Corso Giovanni Agnelli 200, I – 10135 Torino, Italia.\\nwww.abarth.it\\nGegründet 1949 von Carlo Abarth. Seit 2007 Teil von Fiat zur Produktion und Vertrieb von sportlichen Versionen von Fiat-Modellen.\",\"EN\":\"Abarth & C. S.p.A., Corso Giovanni Agnelli 200, I – 10135 Torino, Italy.\\nwww.abarth.it\",\"ES\":\"Abarth & C. S.p.A., Corso Giovanni Agnelli 200, I – 10135 Torino, Italia.\\nwww.abarth.it\",\"FR\":\"Abarth & C. S.p.A., Corso Giovanni Agnelli 200, I – 10135 Torino, Italie.\\nwww.abarth.it\",\"PO\":\"Abarth & C. S.p.A., Corso Giovanni Agnelli 200, I – 10135 Torino, Itàlia.\\nwww.abarth.it\"},\"doneyears\":[[1955,1961],[1963,1971],[2008,2020]]},{\"idmake\":2,\"name\":\"AC\",\"important\":0,\"description\":{\"DE\":\"AC Cars, Siemensstrasse, 75334 Straubenhart, Deutschland.\\nwww.ac-automotive.com\\nGegründet 1911 in Grossbritannien. Fertigung des Mk VI heute in Heya, Deutschland.\",\"EN\":\"AC Cars, Siemensstrasse, 75334 Straubenhart, Germany.\\nwww.ac-automotive.com\",\"ES\":\"AC Cars, Siemensstrasse, 75334 Straubenhart, Alemania.\\nwww.ac-automotive.com\",\"FR\":\"AC Cars, Siemensstrasse, 75334 Straubenhart, Allemagne.\\nwww.ac-automotive.com\\nCréée en 1911, en Grande Bretagne. Fabrication de la Mk VI aujourd'hui à Heya, Allemagne.\",\"PO\":\"AC Cars, Siemensstrasse, 75334 Straubenhart, Alemanha.\\nwww.ac-automotive.com\"},\"doneyears\":[[1948,1963],[1966,1991],[1993,1996],[2000,2003],[2007,2008],[2010,2019]]},{\"idmake\":3,\"name\":\"Acura\",\"important\":0,\"description\":{\"DE\":\"Acura Division, American Honda Motor Co., Inc., 1919 Torrance Boulevard, Torrance, California 90 501.2746, USA.\\nwww.acura.com\\nSeit 1986 als Premium-Marke von Honda auf dem amerikanischen Markt mit eigenständigen Modellen.\",\"EN\":\"Acura Division, American Honda Motor Co., Inc., 1919 Torrance Boulevard, Torrance, California 90 501.2746, USA.\\nwww.acura.com\",\"ES\":\"Acura Division, American Honda Motor Co., Inc., 1919 Torrance Boulevard, Torrance, California 90 501.2746, EE. UU.\\nwww.acura.com\",\"FR\":\"Acura Division, American Honda Motor Co., Inc., 1919 Torrance Boulevard, Torrance, California 90 501.2746, USA.\\nwww.acura.com\\nDepuis 1986, marque haut de gamme de Honda sur le marché américain, avec ses propres modèles.\",\"PO\":\"Acura Division, American Honda Motor Co., Inc., 1919 Torrance Boulevard, Torrance, California 90 501.2746, EUA.\\nwww.acura.com\",\"RU\":null},\"doneyears\":[[1966,1966],[1988,1988],[1991,1991],[1996,2003],[2007,2020]]}";

Now when I parse this string that contains JSON by this statement:

var jObject = JObject.Parse(receivedResult);

I am getting following formatted output:

{
  "method": "_result",
  "params": [
    [
      {
        "idmake": 1,
        "name": "Abarth",
        "important": 0,
        "description": {
          "DE": "Abarth & C. S.p.A., Corso Giovanni Agnelli 200, I – 10135 Torino, Italia.\nwww.abarth.it\nGegründet 1949 von Carlo Abarth. Seit 2007 Teil von Fiat zur Produktion und Vertrieb von sportlichen Versionen von Fiat-Modellen.",
          "EN": "Abarth & C. S.p.A., Corso Giovanni Agnelli 200, I – 10135 Torino, Italy.\nwww.abarth.it",
          "ES": "Abarth & C. S.p.A., Corso Giovanni Agnelli 200, I – 10135 Torino, Italia.\nwww.abarth.it",
          "FR": "Abarth & C. S.p.A., Corso Giovanni Agnelli 200, I – 10135 Torino, Italie.\nwww.abarth.it",
          "PO": "Abarth & C. S.p.A., Corso Giovanni Agnelli 200, I – 10135 Torino, Itàlia.\nwww.abarth.it"
        },
        "doneyears": [
          [
            1955,
            1961
          ],
          [
            1963,
            1971
          ],
          [
            2008,
            2020
          ]
        ]
      },
      {
        "idmake": 2,
        "name": "AC",
        "important": 0,
        "description": {
          "DE": "AC Cars, Siemensstrasse, 75334 Straubenhart, Deutschland.\nwww.ac-automotive.com\nGegründet 1911 in Grossbritannien. Fertigung des Mk VI heute in Heya, Deutschland.",
          "EN": "AC Cars, Siemensstrasse, 75334 Straubenhart, Germany.\nwww.ac-automotive.com",
          "ES": "AC Cars, Siemensstrasse, 75334 Straubenhart, Alemania.\nwww.ac-automotive.com",
          "FR": "AC Cars, Siemensstrasse, 75334 Straubenhart, Allemagne.\nwww.ac-automotive.com\nCréée en 1911, en Grande Bretagne. Fabrication de la Mk VI aujourd'hui à Heya, Allemagne.",
          "PO": "AC Cars, Siemensstrasse, 75334 Straubenhart, Alemanha.\nwww.ac-automotive.com"
        },
        "doneyears": [
          [
            1948,
            1963
          ],
          [
            1966,
            1991
          ],
          [
            1993,
            1996
          ],
          [
            2000,
            2003
          ],
          [
            2007,
            2008
          ],
          [
            2010,
            2019
          ]
        ]
      },
      {
        "idmake": 3,
        "name": "Acura",
        "important": 0,
        "description": {
          "DE": "Acura Division, American Honda Motor Co., Inc., 1919 Torrance Boulevard, Torrance, California 90 501.2746, USA.\nwww.acura.com\nSeit 1986 als Premium-Marke von Honda auf dem amerikanischen Markt mit eigenständigen Modellen.",
          "EN": "Acura Division, American Honda Motor Co., Inc., 1919 Torrance Boulevard, Torrance, California 90 501.2746, USA.\nwww.acura.com",
          "ES": "Acura Division, American Honda Motor Co., Inc., 1919 Torrance Boulevard, Torrance, California 90 501.2746, EE. UU.\nwww.acura.com",
          "FR": "Acura Division, American Honda Motor Co., Inc., 1919 Torrance Boulevard, Torrance, California 90 501.2746, USA.\nwww.acura.com\nDepuis 1986, marque haut de gamme de Honda sur le marché américain, avec ses propres modèles.",
          "PO": "Acura Division, American Honda Motor Co., Inc., 1919 Torrance Boulevard, Torrance, California 90 501.2746, EUA.\nwww.acura.com",
          "RU": null
        },
        "doneyears": [
          [
            1966,
            1966
          ],
          [
            1988,
            1988
          ],
          [
            1991,
            1991
          ],
          [
            1996,
            2003
          ],
          [
            2007,
            2020
          ]
        ]
      }
    ]
  ],
  "seq": 100
}

I want to loop through each array of params JArray, take values of every name field, and store these all values in one list.

I was able to take the name value from the first array which is Abarth by the following code:

 JArray paramsArray = (JArray)jObject["params"];
            List<string> listName = new List<string>();

            foreach(JToken param in paramsArray)
            {
                name = (string)param[0]["name"]; // Here I got the target result name="Abarth" from index 0 of array
                listName.Add(name);

                foreach(var item in listName)
                {
                    tbResultCreateNewAd.Text = item.ToString();
                }
                
            }

I was wondering how can I loop through other arrays and take name values? I was trying with for loop but unsuccessfully because I am totally new to working with Json. Thank you all

CodePudding user response:

I've found out how can be done after spending a while, through the while loop I hardcoded the last index number of child arrays, and then do the iteration over all, maybe there exists different solution for this, but for now this logic for me works, combobox is filling after click event is fired properly.

Here is the solution:

  var jObject = JObject.Parse(receivedResult);
                JArray paramsArray = (JArray)jObject["params"];
                foreach(JToken param in paramsArray)
                {
                    int i = 0;
                    while(i<614) // 614 is hardcoded number for last array index
                    { 
                        name = param[i]["name"].ToString();
                        listName.Add(name);
                        i  ;
                       
                    }
                    break;
                }
cbMake.DataSource = listName;
  • Related