C#JSON字符串转JSON对象
往往会有这么⼀种情景:有⼀个⾃定义的json或者json数组的字符串,希望转换成对应的对象。由于程序对于⾃定义的JSON格式并不清楚,所以直接ToObject()或者ToList()均会失败。
这时我们要做的就是让程序知道我们⾃定义的json到底是什么样的。如下:
//数据库中某个字段内容如下,NeedToJson⽤于接收该字段值
// [{"ProjectName":"锅炉整体是否有明显的变形、保温是否完好","ShowType":0,"IsMust":1},{"ProjectName":"锅炉煤量(吨)","ShowType":1,"IsMust":1},{"ProjectName":"锅炉图⽚","ShowType":2,"IsMust":1}] public class NeedToJsonDbo
{
//省略部分代码。。。
string NeedToJson= 数据库操作,接受 NeedToJson 字段值;
List<ToJsonDetailJson> strToJson = NeedToJson.ToList<ToJsonDetailJson>();
//省略部分代码。。。
}
//声明⼀个⾃定义json内容⼀致的类
public class ToJsonDetailJson
{
//属性的名字,必须与json格式字符串中的"key"值⼀样。
public string ProjectName { get; set; }
public string ShowType { get; set; }
public string IsMust { get; set; }
}
以上在对象属性均为简单数据类型时没有问题,但是当对象的某个数据仍未对象时就不⾏了。
这时可以引⼊ Newtonsoft.Json.Linq
例如请求国家⽓象局获得城市天⽓接⼝获得数据(接⼝如下)
获得的数据如下:
{
"data": {
"message": "success感谢⼜拍云(upyun)提供CDN赞助",
"status": 200,
"date": "20191129",
"time": "2019-11-29 09:51:46",
"cityInfo": {
"city": "青岛市",
"citykey": "101120201",
"parent": "⼭东",
"updateTime": "09:09"
},
"data": {
"shidu": "63%",
"pm25": 30,
"pm10": 63,
"quality": "良",
"wendu": "1",
"ganmao": "极少数敏感⼈应减少户外活动",
"forecast": [
{
"date": "29",
"high": "⾼温 6℃",
"low": "低温 2℃",
"ymd": "2019-11-29",
"week": "星期五",
"sunrise": "06:47",
"sunset": "16:45",
"aqi": 51,
"fx": "北风",
"fl": "3-4级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
},
{
"date": "30",
"high": "⾼温 7℃",
"low": "低温 4℃",
"ymd": "2019-11-30",
"week": "星期六",
"sunrise": "06:48",
"sunset": "16:45",
"aqi": 67,
"fx": "北风",
"fl": "3-4级",
"type": "阵⾬",
"notice": "阵⾬来袭,出门记得带伞"
},
{
"date": "01",
"high": "⾼温 7℃",
"low": "低温 2℃",
"ymd": "2019-12-01",
"week": "星期⽇",
"sunrise": "06:49",
"sunset": "16:44",
"aqi": 78,
"fx": "北风",
"fl": "3-4级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
},
{
"high": "⾼温 5℃",
"low": "低温 -1℃",
"ymd": "2019-12-02",
"week": "星期⼀",
"sunrise": "06:50",
"sunset": "16:44",
"aqi": 67,
"fx": "北风",
"fl": "5-6级",
"type": "晴",
"notice": "愿你拥有⽐阳光明媚的⼼情" },
{
"date": "03",
"high": "⾼温 7℃",
"low": "低温 0℃",
"ymd": "2019-12-03",
"week": "星期⼆",
"sunrise": "06:51",
"sunset": "16:44",
"aqi": 82,
"fx": "西南风",
"fl": "3-4级",
json转换对象"type": "晴",
"notice": "愿你拥有⽐阳光明媚的⼼情" },
{
"date": "04",
"high": "⾼温 8℃",
"low": "低温 1℃",
"ymd": "2019-12-04",
"week": "星期三",
"sunrise": "06:52",
"sunset": "16:44",
"aqi": 78,
"fx": "北风",
"fl": "3-4级",
"type": "晴",
"notice": "愿你拥有⽐阳光明媚的⼼情" },
{
"date": "05",
"high": "⾼温 7℃",
"low": "低温 0℃",
"ymd": "2019-12-05",
"week": "星期四",
"sunrise": "06:53",
"sunset": "16:44",
"fx": "南风",
"fl": "3-4级",
"type": "晴",
"notice": "愿你拥有⽐阳光明媚的⼼情" },
{
"date": "06",
"high": "⾼温 6℃",
"low": "低温 0℃",
"ymd": "2019-12-06",
"week": "星期五",
"sunrise": "06:54",
"sunset": "16:44",
"fx": "北风",
"fl": "<3级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰" },
{
"date": "07",
"high": "⾼温 5℃",
"low": "低温 0℃",
"ymd": "2019-12-07",
"week": "星期六",
"sunrise": "06:54",
"sunset": "16:44",
"fx": "北风",
"fl": "3-4级",
"type": "晴",
"notice": "愿你拥有⽐阳光明媚的⼼情" },
{
"date": "08",
"high": "⾼温 6℃",
"low": "低温 1℃",
"ymd": "2019-12-08",
"week": "星期⽇",
"sunrise": "06:55",
"sunset": "16:44",
"fx": "西北风",
"fl": "3-4级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰" },
{
"date": "09",
"high": "⾼温 8℃",
"low": "低温 3℃",
"ymd": "2019-12-09",
"week": "星期⼀",
"sunrise": "06:56",
"sunset": "16:44",
"fx": "西风",
"type": "晴",
"notice": "愿你拥有⽐阳光明媚的⼼情"
},
{
"date": "10",
"high": "⾼温 10℃",
"low": "低温 3℃",
"ymd": "2019-12-10",
"week": "星期⼆",
"sunrise": "06:57",
"sunset": "16:44",
"fx": "西南风",
"fl": "3-4级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
},
{
"date": "11",
"high": "⾼温 9℃",
"low": "低温 1℃",
"ymd": "2019-12-11",
"week": "星期三",
"sunrise": "06:58",
"sunset": "16:44",
"fx": "西北风",
"fl": "3-4级",
"type": "晴",
"notice": "愿你拥有⽐阳光明媚的⼼情"
},
{
"date": "12",
"high": "⾼温 7℃",
"low": "低温 1℃",
"ymd": "2019-12-12",
"week": "星期四",
"sunrise": "06:58",
"sunset": "16:44",
"fx": "东北风",
"fl": "<3级",
"type": "晴",
"notice": "愿你拥有⽐阳光明媚的⼼情"
},
{
"date": "13",
"high": "⾼温 7℃",
"low": "低温 2℃",
"ymd": "2019-12-13",
"week": "星期五",
"sunrise": "06:59",
"sunset": "16:45",
"fx": "南风",
"fl": "<3级",
"type": "晴",
"notice": "愿你拥有⽐阳光明媚的⼼情"
}
],
"yesterday": {
"date": "28",
"high": "⾼温 5℃",
"low": "低温 0℃",
"ymd": "2019-11-28",
"week": "星期四",
"sunrise": "06:46",
"sunset": "16:45",
"aqi": 39,
"fx": "北风",
"fl": "4-5级",
"type": "晴",
"notice": "愿你拥有⽐阳光明媚的⼼情"
}
}
},
"msg": "请求成功!",
"code": 20000,
"success": true
}
using Newtonsoft.Json.Linq;
//---省略部分代码---//
var data=请求国家⽓象局接⼝获得数据。如上。
var weatherData = JObject.Parse(data);
var weatherData_data = weatherData["data"];//此时不能⽤'.'操作获取到data属性值,但是可以通过属性名获取属性值。//---省略部分代码---//
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论