【问题标题】:JsonConvert.DeserializeObject always sending null valueJsonConvert.DeserializeObject 总是发送空值
【发布时间】:2016-10-14 22:07:45
【问题描述】:

我需要将数据从 Json(String) 转换为对象以使用其属性。我一直有一个空值。 我检查了 Json 文件和我的课程,但没有看到这个问题的可能原因。你能帮帮我吗?`

这是来自 Json 文件的一部分:

{


"global": {
  "@context": "http://environment.data.gov.uk/flood-monitoring/meta/context.jsonld",

"meta": {
    "publisher": "Environment Agency",
    "licence": "http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/",
    "documentation": "http://environment.data.gov.uk/flood-monitoring/doc/reference",
    "version": "0.6.1",
    "comment": "Status: Beta service",
    "hasFormat": [
        "http://environment.data.gov.uk/flood-monitoring/id/stations.csv"
    ]
},
"items": [
    {
        "@id": "http://environment.data.gov.uk/flood-monitoring/id/stations/1029TH",
        "RLOIid": "7041",
        "catchmentName": "Cotswolds",
        "dateOpened": "1994-01-01",
        "easting": 417990,
        "label": "Bourton Dickler",
        "lat": 51.874767,
        "long": -1.740083,
        "measures": [
            {
                "@id": "http://environment.data.gov.uk/flood-monitoring/id/measures/1029TH-level-downstage-i-15_min-mASD",
                "parameter": "level",
                "parameterName": "Water Level",
                "period": 900,
                "qualifier": "Downstream Stage",
                "unitName": "mASD"
            },
            {
                "@id": "http://environment.data.gov.uk/flood-monitoring/id/measures/1029TH-level-stage-i-15_min-mASD",
                "parameter": "level",
                "parameterName": "Water Level",
                "period": 900,
                "qualifier": "Stage",
                "unitName": "mASD"
            }
        ],
        "northing": 219610,
        "notation": "1029TH",
        "riverName": "Dikler",
        "stageScale": "http://environment.data.gov.uk/flood-monitoring/id/stations/1029TH/stageScale",
        "stationReference": "1029TH",
        "town": "Little Rissington",
        "wiskiID": "1029TH"
    },
    {
        "@id": "http://environment.data.gov.uk/flood-monitoring/id/stations/E2043",
        "RLOIid": "6022",
        "catchmentName": "Welland",
        "dateOpened": "1992-01-01",
        "datumOffset": 2,
        "easting": 528000,
        "label": "Surfleet Sluice",
        "lat": 52.845991,
        "long": -0.100848,
        "measures": [
            {
                "@id": "http://environment.data.gov.uk/flood-monitoring/id/measures/E2043-level-stage-i-15_min-mASD",
                "parameter": "level",
                "parameterName": "Water Level",
                "period": 900,
                "qualifier": "Stage",
                "unitName": "mASD"
            }
        ],
        "northing": 329300,
        "notation": "E2043",
        "riverName": "River Glen",
        "stageScale": "http://environment.data.gov.uk/flood-monitoring/id/stations/E2043/stageScale",
        "stationReference": "E2043",
        "town": "Surfleet Seas End",
        "wiskiID": "L31004"
    },

这些是类:

    namespace TechnicalTestZK
{
class Program
{
    static void Main(string[] args)
    {

        var json = File.ReadAllText("C:/Users/Zied/Desktop/Json.txt");

        var ab = JsonConvert.DeserializeObject<Global>(json);
        Console.WriteLine(ab.Items[0].RiverName); 

        //Console.WriteLine(GET("http://environment.data.gov.uk/flood-monitoring/id/floods"));
        Console.ReadLine();

    }

    static string GET(string url)
    {
        System.Net.HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        try
        {
            WebResponse response = request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                return reader.ReadToEnd();
            }
        }
        catch (WebException ex)
        {
            WebResponse errorResponse = ex.Response;
            using (Stream responseStream = errorResponse.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
                String errorText = reader.ReadToEnd();
                // log errorText
            }
            throw;
        }
    }
}


}


using Newtonsoft.Json;


namespace TechnicalTestZK
{
    public class Meta
{

    [JsonProperty("publisher")]
    public string Publisher { get; set; }

    [JsonProperty("licence")]
    public string Licence { get; set; }

    [JsonProperty("documentation")]
    public string Documentation { get; set; }

    [JsonProperty("version")]
    public string Version { get; set; }

    [JsonProperty("comment")]
    public string Comment { get; set; }

    [JsonProperty("hasFormat")]
    public string[] HasFormat { get; set; }
    }
}


using Newtonsoft.Json;


namespace TechnicalTestZK
{
    public class Measure
{

    [JsonProperty("@id")]
    public string Id { get; set; }

    [JsonProperty("parameter")]
    public string Parameter { get; set; }

    [JsonProperty("parameterName")]
    public string ParameterName { get; set; }

    [JsonProperty("period")]
    public int Period { get; set; }

    [JsonProperty("qualifier")]
    public string Qualifier { get; set; }

    [JsonProperty("unitName")]
    public string UnitName { get; set; }
}
}

using Newtonsoft.Json;

namespace TechnicalTestZK
{

    public class Item
    {

        [JsonProperty("@id")]
        public string Id { get; set; }

        [JsonProperty("RLOIid")]
        public string RLOIid { get; set; }

        [JsonProperty("catchmentName")]
        public string CatchmentName { get; set; }

        [JsonProperty("dateOpened")]
        public string DateOpened { get; set; }

        [JsonProperty("easting")]
        public int Easting { get; set; }

        [JsonProperty("label")]
        public string Label { get; set; }

        [JsonProperty("lat")]
        public string Lat { get; set; }

        [JsonProperty("long")]
        public string Long { get; set; }

        [JsonProperty("measures")]
        public Measure[] Measures { get; set; }

        [JsonProperty("northing")]
        public int Northing { get; set; }

        [JsonProperty("notation")]
        public string Notation { get; set; }

        [JsonProperty("riverName")]
        public string RiverName { get; set; }

        [JsonProperty("stageScale")]
        public string StageScale { get; set; }

        [JsonProperty("stationReference")]
        public string StationReference { get; set; }

        [JsonProperty("town")]
        public string Town { get; set; }

        [JsonProperty("wiskiID")]
        public string WiskiID { get; set; }

        [JsonProperty("datumOffset")]
        public float? DatumOffset { get; set; }

        [JsonProperty("downstageScale")]
        public string DownstageScale { get; set; }
    }
}

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace TechnicalTestZK
{
internal class Global
{
    [JsonProperty("@context")]
    public string @context { get; set; }

    [JsonProperty("meta")]
    public Meta Meta { get; set; }

    [JsonProperty("items")]
    public Item[] Items { get; set; }
}
}

【问题讨论】:

    标签: c# json json.net data-conversion


    【解决方案1】:

    您尝试反序列化的 Json 文件比 Global 高一个“级别”,这是您尝试反序列化的内容。相反,创建一个看起来像这样的新类:

    public class GlobalContainer
    {
        [JsonProperty("global")]
        public Global global { get; set; }
    }
    

    并通过调用 JsonConvert.DeserializeObject&lt;GlobalContainer&gt;(json) 反序列化为它

    【讨论】:

    • 非常感谢。你的回答解决了我的问题
    • @zied - 如果这回答了您的问题,请mark it as such 以便其他人知道将其用作参考。
    【解决方案2】:

    或者您可以使用 json 2 # 解析器,例如 http://json2csharp.com ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-26
      • 2021-12-21
      • 2015-10-27
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      相关资源
      最近更新 更多