Julius Alexander
Julius Alexander

Reputation: 501

json deserialize into class and access results in c#

I'm deserializing json into a class and it seems to work, but I can't seem to figure out how to access the results. I am instantiating the class and then attempting to place the deserialized json into it but then the class containers are out of reach. I'm not sure how to work with the individual containers.

Here is the class

public class geocodes
{
    public Boolean outcome { get; set; }

    public double xcord { get; set; }

    public double ycord { get; set; }
}

class jsonresult
{
    public class Rootobject
    {
        public Spatialreference spatialReference { get; set; }
        public Candidate[] candidates { get; set; }
    }

    public class Spatialreference
    {
        public int wkid { get; set; }
        public int latestWkid { get; set; }
    }

    public class Candidate
    {
        public string address { get; set; }
        public Location location { get; set; }
        public int score { get; set; }
        public Attributes attributes { get; set; }
        public Extent extent { get; set; }
    }

    public class Location
    {
        public float x { get; set; }
        public float y { get; set; }
    }

    public class Attributes
    {
    }

    public class Extent
    {
        public float xmin { get; set; }
        public float ymin { get; set; }
        public float xmax { get; set; }
        public float ymax { get; set; }
    }
}

Here is the code I populate the class with

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
    StreamReader reader = new StreamReader(response.GetResponseStream());
    string responseString = reader.ReadToEnd();

    jsonresult results = System.Text.Json.JsonSerializer.Deserialize<jsonresult>(responseString);
    ...
}

I'd like to do a foreach through every root container or even spatial reference (because it will often return more than one) I would even be happy if I just pulled that portion of the json into a class, but I can't figure out how.

Here is the json:

{
 "spatialReference": {
  "wkid": 2253,
 "latestWkid": 2253
 },
 "candidates": [
  {
   "address": "816 Example ST",
   "location": {
   "x": 13288754.574891519,
    "y": 288356.77197193989
  },
   "score": 100,
  "attributes": {

  },
   "extent": {
    "xmin": 13288098.406912517,
    "ymin": 287700.60399293725,
    "xmax": 13289410.742870521,
    "ymax": 289012.93995094253
   }
  }
 ]
}

Upvotes: 0

Views: 356

Answers (1)

Peter Csala
Peter Csala

Reputation: 22819

The root cause of the problem is the extra (unnecessary) layer: jsonresult.

jsonresult does not have any property that's why System.Text.Json can not correctly deserialize data into it. In other words after JsonSerializer has created a new instance of the jsonresult class by calling its parameterless constructor it can not assign the read value to any of its public properties.


If you remove the jsonresult surrounding class and use RootObject during Deserialize then after JsonSerializer created a RootObject instance then it can populate its properties with data by using reflection (and name based matching).

Upvotes: 1

Related Questions