关于c#中json文件格式转string的方法

首先你需要接收一个json的变量,
string jsonStr = "";

之后就是处理这个jsonStr字符串了,

JObject jobject = JObject.Parse(jsonStr);//json格式转换
string results = jobject["results"].ToString();

这样就得到了json文件中你需要的属性results的值,

这里如果这个值results任然是一个json格式,而且是以数组的形式,需要再转换

"results": [{"electioncandidatestatus": null, "incumbent": true, "candidate_id": 176815, "district": "56", "party": "Republican", "photo": null, "office": "State House", "state_id": "ME", "match": 2.0, "name": "Richard Mason"}, {"electioncandidatestatus": null, "incumbent": null, "candidate_id": 176815, "district": "56", "party": "Republican", "photo": null, "office": "State House", "state_id": "ME", "match": 2.0, "name": "Richard Mason"}, {"electioncandidatestatus": null, "incumbent": true, "candidate_id": 128845, "district": null, "party": "", "photo": null, "office": "Senior Judge of the U.S. District Court", "state_id": "CO", "match": 1.3, "name": "Richard Mastch"}]

比如是以上的格式

JArray jarray = JArray.Parse(results);
再遍历jarray了,同上,
 for (int i = 0; i < jarray.Count; i++)
                                {
                                    string candidate_id = jarray[i]["candidate_id"].ToString();
                                    string votesmartdistrict = jarray[i]["district"].ToString();
                                    string votesmartoffice = jarray[i]["office"].ToString();
                                }
到这里就遍历完了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值