C# 利用Newtonsoft.Json将json转换为实体对象

需要先引用命名空间
Newtonsoft.Json 和 Newtonsoft.Json.Linq
1.可自行网上下载 Newtonsoft.Json.dll ,在项目中添加引用
2.或者是直接使用VS的内置NuGet包管理器中搜索Newtonsoft.Json 安装到指定项目

1.针对简单的json串

需要处理的json串
“{
“message”: “操作成功”,
“data”: “This is a data”,
“success”: “true”
}”
解析:

			  string json = "{\"message\": \"操作成功\",\"data\": \"This is a data\",\"success\": \"true\"}";
            Object obj = JsonConvert.DeserializeObject(json);
            Newtonsoft.Json.Linq.JObject js = obj as Newtonsoft.Json.Linq.JObject;//把上面的obj转换为 Jobject对象
            string data = js["data"].ToString();
  

2.针对简单的json串数组

需要处理的json串数组
[
{
“deviceID”: “110722”,
“discloseTime”: “2019-03-08 01:00:00”,
“projectId”: “24”
},
{
“deviceID”: “110732”,
“discloseTime”: “2019-03-08 15:40:12”,
“projectId”: “24”
},
{
“deviceID”: “110735”,
“discloseTime”: “2019-03-08 16:40:12”,
“projectId”: “24”
}
]
解析:


            string json = "[{\"deviceID\": \"110722\",\"discloseTime\": \"2019-03-08 01:00:00\",\"projectId\": \"24\"},{\"deviceID\": \"110732\",\"discloseTime\": \"2019-03-08 15:40:12\",\"projectId\": \"24\"},{\"deviceID\": \"110735\",\"discloseTime\": \"2019-03-08 16:40:12\",\"projectId\": \"24\"}]";
            Object obj = JsonConvert.DeserializeObject(json);
            Newtonsoft.Json.Linq.JArray jarray = obj as Newtonsoft.Json.Linq.JArray;//把上面的obj转换为 Jobject对象
            List bodyList = new List();
            for (int i = 0; i < jarray.Count; i++)
            {
                string listdata = jarray[0].ToString();
                Object obj1 = JsonConvert.DeserializeObject(listdata);
                Newtonsoft.Json.Linq.JObject js1 = obj1 as Newtonsoft.Json.Linq.JObject;//把上面的obj转换为 Jobject对象
                Body body = new Body
                {
                    DeviceID= js1["deviceID"].ToString(),
                    DiscloseTime = js1["discloseTime"].ToString(),
                    ProjectId = js1["projectId"].ToString()
                };
                bodyList.Add(body);
            }

3.针对复合的json串

需要处理的json数据
{
“message”: “操作成功”,
“data”: [
{
“deviceID”: “110722”,
“discloseTime”: “2019-03-08 01:00:00”,
“projectId”: “24”
},
{
“deviceID”: “110732”,
“discloseTime”: “2019-03-08 15:40:12”,
“projectId”: “24”
},
{
“deviceID”: “110735”,
“discloseTime”: “2019-03-08 16:40:12”,
“projectId”: “24”
}
],
“success”: ”true“
}

解析:

			 List bodyList = new List();
            Object obj = JsonConvert.DeserializeObject(json);
            Newtonsoft.Json.Linq.JObject js = obj as Newtonsoft.Json.Linq.JObject;//把上面的obj转换为 Jobject对象
            JArray jarray = (JArray)js["data"];
            for (int i = 0; i < jarray.Count; i++)
            {
                string data = jarray[0].ToString();
                Object obj1 = JsonConvert.DeserializeObject(data);
                Newtonsoft.Json.Linq.JObject js1 = obj1 as Newtonsoft.Json.Linq.JObject;//把上面的obj转换为 Jobject对象
                Body body = new Body
                {
                    DeviceID = js1["deviceID"].ToString(),
                    DiscloseTime = js1["discloseTime"].ToString(),
                    ProjectId = js1["projectId"].ToString()
                };
                bodyList.Add(body);
            }

你可能感兴趣的:(C#)