0

I need to convert data from Json(String) to an object to use its properties. I am keep having a null value. I checked the Json file and my classes and I don't see the possible reason for this problem. Can you please help me?`

This a part from the Json file:

{


"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"
    },

These are the classes:

    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; }
}
}

2 Answers 2

2

The Json file you are trying to deserialize is one "level" higher than Global, which is what you are trying to deserialize into. Instead, make a new class that looks something like this:

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

And deserialize into that by calling JsonConvert.DeserializeObject<GlobalContainer>(json)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. Your answer resolved my problem
@zied - if this answers your question, please mark it as such so others can know to use it as a reference.
0

Or you can use a json 2 # parser like http://json2csharp.com ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.