这是我的示例字符串
{
"aaa": "0",
"nnn": "Ok",
"rrr": [
{
"id": "0",
"name": "Reserved",
"desc": "Reserved",
"shortdesc": "",
"price": "07.80",
"isvariableprice": "0",
"taxcategoryid": "0",
"istaxinclusive": "0",
"brandid": "1",
"isnetsalesexempt": "0",
"itemtype": "0",
"skus": [],
"modifiergroups": [],
"prices": [],
"pricerules": [],
"categories": [],
"nutrition": [
{
"id": "1",
"amount": "10.0000"
}
]
}
]
}
我只想从示例json响应中检索“id”和“price”。 JSON更多的是这个并且有多个id; s和价格......我如何设置一个循环来检索数组中的所有ID。请帮忙。
这就是我现在所处的位置。
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
Console.WriteLine("Response stream received.");
string resultString = readStream.ReadToEnd();
//var ser = new DataContractJsonSerializer(typeof(RootObject));
var stream = new MemoryStream(Encoding.Unicode.GetBytes(resultString));
dynamic data = JsonObject.Parse(resultString);
Console.WriteLine(data.id); //??????????
readStream.Close();
}
这段代码在“数据”中给出了上面的字符串,但我想拉“id”和“price”
事先你所有的大师答案 0 :(得分:0)
使用JSON http://json.codeplex.com/
时,Json.Net将让您的生活更轻松使用Json.Net。
您可以以动态方式或强类型方式执行此操作。
动态:
var dynJson = JsonConvert.DeserializeObject<dynamic>(yourJsonString);
//for simple values
Console.WriteLine(dynJson.aaa);
Console.WriteLine(dynJson.nnn);
//for list values
foreach(var r in dynJson.rrr){
//for simple values in rrr
Console.WriteLine(r.id);
//for list values in rrr
foreach(var price in rrr.prices)
Console.WriteLine(price)
}