简易版
var str = "{\"nick\":\"姚纯英\",\"gmt_created\":\"2016-08-23 10:27:29\"}";
var obj = JsonConvert.DeserializeObject(str);
Console.WriteLine(obj.GetType()); //Newtonsoft.Json.Linq.JObject
foreach (var x in obj as JObject)
{
Console.WriteLine("{0} {1}", x.Key, x.Value);
}
实际版本
原JSON字符串
{
"errcode": 0,
"result": [{
"check_type": "OnDuty",
"group_id": 532670037,
"id": 142209165801,
"is_rest": "N",
"plan_check_time": "2020-09-04 07:50:00",
"shift_id": 534475050,
"shift_version": 510842386,
"userid": "012524601729258924",
"work_date": "2020-09-04 00:00:00"
}, {
"check_type": "OffDuty",
"group_id": 532670037,
"id": 142209165802,
"is_rest": "N",
"plan_check_time": "2020-09-04 12:00:00",
"shift_id": 534475050,
"shift_version": 510842386,
"userid": "012524601729258924",
"work_date": "2020-09-04 00:00:00"
}, {
"check_type": "OffDuty",
"group_id": 532670037,
"id": 142209165276,
"is_rest": "N",
"plan_check_time": "2020-09-04 18:00:00",
"shift_id": 534475050,
"shift_version": 510842386,
"userid": "2410456929267501",
"work_date": "2020-09-04 00:00:00"
}],
"success": true,
"request_id": "roz8ezomhjsz"
}
遍历JSON
#region 钉钉审批详情
IDingTalkClient client03 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/get");
OapiProcessinstanceGetRequest req03 = new OapiProcessinstanceGetRequest();
req03.ProcessInstanceId = "6680a656-376e-4eb8-b2da-c3f41cdaa4a1";
req03.SetHttpMethod("POST");
OapiProcessinstanceGetResponse rsp03 = client03.Execute(req03, dingAccessToken);
string sch03 = rsp03.Body;
//JObject jo102 = (JObject)JsonConvert.DeserializeObject(sch02);//转换为json对象
StringBuilder sbInsert03 = new StringBuilder();
var o03 = JObject.Parse(sch03);
foreach (JToken child in o03.Children())
{
//string a = child["errmsg"].ToString();
//var property1 = child as JProperty;
//MessageBox.Show(property1.Name + ":" + property1.Value);
foreach (JToken grandChild in child.Children())
{
//var property = grandChild as ResultInfo01;
//property.check_type;
string key01 = grandChild.Path.ToString();
if (key01 == "process_instance")
{
foreach (var x in grandChild as JObject)//遍历JSON字符串,取键值
{
Console.WriteLine("{0} {1}", x.Key, x.Value);
sbInsert.Append(" insert into DDPaiBan (userid,is_rest,work_date) values ('" + x.Key + "'," + x.Value + ",'" + x.Value + "','" + x.Value + "','" + x.Value + "') ; ");
}
//JArray jo2 = (JArray)JsonConvert.DeserializeObject(grandChild.ToString());//转换为json对象6680a656-376e-4eb8-b2da-c3f41cdaa4a1
//for (int i = 0; i < jo2.Count; i++)
//{
// string list = jo2[i]["title"].ToString();//
// if (sbInsert.ToString().IndexOf(list) < 0)//如果员工ID不存在,则追加ddpaiban
// {
// sbInsert.Append(" insert into DDPaiBan (userid,is_rest,work_date) values ('" + list + "'," + list + ",'" + list + "','" + list + "','" + list + "') ; ");
// }
//}
}
//string str02 = grandChild.ToString();
string str03 = "";
}
}
string str012 = "";
//int idel = SqlHelper.ExecuteNonQuery(delSQL, CommandType.Text);
//int iinsert = SqlHelper.ExecuteNonQuery(sbInsert.ToString(), CommandType.Text);
#endregion