使用JSON.NET循环遍历json结果 [英] Loop through json result using JSON.NET
本文介绍了使用JSON.NET循环遍历json结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用下面的代码来获取使用JSON.NET的大厅列表。
I am using below code to get list of halls using JSON.NET.
我想知道如何我可以遍历结果来获取result_name吗?
I want to know how can I loop through the result to get result_name?
这里的代码是示例结果。
here is the code follow by sample result.
var client = new HttpClient();
client.BaseAddress = new Uri("https://www.domain.com/ws/populate_halls_in_location.php");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("https://www.domain.com/ws/populate_halls_in_location.php");
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
dynamic storydata = JsonConvert.DeserializeObject(data);
*************************
*************************
*************************
LOOP THROUGH THE RESULT FROM storedata to get hall_name
*************************
*************************
*************************
};
这里是样本输出:
[
{
"hall_id":1,
"0":1,
"site_id":7,
"1":7,
"hall_name":"Hall No. 1",
"is_active":1,
"3":1
},
{
"hall_id":2,
"0":2,
"site_id":7,
"1":7,
"hall_name":"Hall No. 2",
"is_active":1,
"3":1
},
{
"hall_id":3,
"0":3,
"site_id":7,
"1":7,
"hall_name":"Hall No. 3",
"is_active":1,
"3":1
}
]
推荐答案
这是一个数组,因此
storydata [i] 应该为你提供元素,其中 i = 0..2 。
This is an array, therefore storydata[i] should give you the elements, where i=0..2.
要访问成员,请尝试
dynamic x = storydata [i] [" 1"] 或 int x等表达式=(int)storydata [i] [" 1"] ,它应该给你7。
To access the members, try expressions like dynamic x = storydata[i]["1"] or int x = (int)storydata[i]["1"], which should give you 7.
这篇关于使用JSON.NET循环遍历json结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文