首先你需要接收一个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();
}
到这里就遍历完了。