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

例如请求国家气象局获得城市天气接口获得数据(接口如下)

http://t.weather.sojson.com/api/weather/city/101120201

获得的数据如下:

{
  "data": {
    "message": "success感谢又拍云(upyun.com)提供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": "阴晴之间,谨防紫外线侵扰"
        },
        {
          "date": "02",
          "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级",
          "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": "西风",
          "fl": "<3级",
          "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属性值,但是可以通过属性名获取属性值。

//---省略部分代码---//
原文地址:https://www.cnblogs.com/dyhao/p/11097233.html