我有以下来自服务器的响应消息作为字符串:
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]]}";
现在,当我通过以下语句解析这个包含JSON的字符串时:
var jObject = JObject.Parse(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.itnGegrü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.comnGegrü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.comnCréé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.comnSeit 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.comnDepuis 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
}
我想循环遍历params
JArray的每个数组,获取每个name
字段的值,并将这些值存储在一个列表中。
我可以通过以下代码从第一个数组Abarth
中获取名称值:
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();
}
}
我想知道如何在其他数组中循环并获取名称值我曾尝试for loop,但没有成功,因为我对与Json合作完全陌生。谢谢大家
我已经找到了如何在花了一段时间后完成,通过while循环,我对子数组的最后一个索引号进行了硬编码,然后进行了迭代,也许对此有不同的解决方案,但目前这种逻辑对我来说是有效的,combobox是在点击事件正确触发后填充的。
以下是解决方案:
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;