Hello, currently I am working on WinForms application that has been communicating with API which is based on websocket as a transport protocol where each message is json encoded object. I want to loop through each array in params JArray, take values of every name field, and store these all values in one list. So here is what I have tried:
What I have tried:
1. here is the response message from server as a 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]]}";
2.Next thing that I've done is parsing this string to be JObject by following statement:
var jObject = JObject.Parse(receivedResult);
3. After parsing 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 take every value of field
name in arrays, and store these all values in 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"];
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?