JsonConvert.DeserializeObject总是发送空值



我需要将数据从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; }
}
}

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

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

并通过调用JsonConvert.DeserializeObject<GlobalContainer>(json)

将其反序列化为

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

相关内容

  • 没有找到相关文章