c#解析json字符串数组_C# 解析json数组

本文提供两个C#使用Json.NET库解析JSON字符串数组的案例。案例一展示了如何从包含用户信息的复杂JSON中提取用户数据,案例二则直接解析用户ID和用户名的JSON数组并存储到字典中。
摘要由CSDN通过智能技术生成

案例一:

string json = "[{"Groupid":"54","groupnum":"66000","groupname":"大唐移动","type":"0","dnsprefix":"","islocal":"1","canshowall":"-1","user":[{"userid":"66001","username":"66001","type":"6","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"0","cstate":"0"},{"userid":"66002","username":"66002","type":"6","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"0","cstate":"0"},{"userid":"66003","username":"66003","type":"6","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"0","cstate":"0"},{"userid":"66004","username":"66004","type":"6","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"0","cstate":"0"},{"userid":"66005","username":"66005","type":"0","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"1","cstate":"0"},{"userid":"66006","username":"66006","type":"0","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"0","cstate":"0"},{"userid":"66007","username":"66007","type":"0","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"0","cstate":"0"},{"userid":"66008","username":"66008","type":"0","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"0","cstate":"0"}],"group":[]}]";

//解析用户组json

private void UpdateGroup_mem_Controls(string json)

{

JArray userArry = (JArray)JsonConvert.DeserializeObject(json);

//获取树形用户json字符串

string userList = userArry[0]["user"].ToString();

//解析用户json信息

JArray jar = (JArray)JsonConvert.DeserializeObject(userList);

for (int i = 0; i < jar.Count; i++)

{

JObject j = JObject.Parse(jar[i].ToString());

string userid = j["userid"].ToString();

string type = j["type"].ToString();

string username = j["username"].ToString();

}

}

----------------------------------------------------------------

案例二:

string json = [{"userid":"66001","username":"66001","type":"6","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"0","cstate":"0"},{"userid":"66002","username":"66002","type":"6","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"0","cstate":"0"},{"userid":"66003","username":"66003","type":"6","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"0","cstate":"0"},{"userid":"66004","username":"66004","type":"6","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"0","cstate":"0"},{"userid":"66005","username":"66005","type":"0","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"1","cstate":"0"},{"userid":"66006","username":"66006","type":"0","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"0","cstate":"0"},{"userid":"66007","username":"66007","type":"0","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"0","cstate":"0"},{"userid":"66008","username":"66008","type":"0","dnsprefix":"","othernumber":"","othername":"","islocal":"1","rstate":"0","cstate":"0"}]

JArray userArry = (JArray)JsonConvert.DeserializeObject(json);

Dictionary dic = new Dictionary();

//获取树形用户json字符串

foreach (var item in userArry)

{

JObject j = JObject.Parse(item.ToString());

dic.Add(j["userid"].ToString(), j["username"].ToString());

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值