1

This is the JSON Response I'm getting,I unable to parse,Please help me.

{
"features": [{
    "attributes": {
        "OBJECTID": 1,
        "schcd": "29030300431",
        "schnm": "UNAIDED GENITALIA LIPS DALMATIA RS"

    },
    "geometry": {
        "x": 8449476.63052563,
        "y": 1845072.4204768054
    }
}]
}
Kruti Patel
  • 1,422
  • 2
  • 23
  • 36
user3728743
  • 41
  • 1
  • 2
  • 9

3 Answers3

0

Here is an answer that might get you started Deserialize JSON with C#

I like more the solution from NewtonSoft: Newtonsoft JSON Deserialize

You can use their JsonConverter to simply serialize/deserialize objects

Here's a simple example: You first need classes that match the structure of your JSON:

public class MyCoolClass
{
   public IEnumerable<Feature> features {get; set;}
}

public class Feature 
{
   public Attributes Attributes {get; set;}
   public Geometry Geometry {get; set;} 
}

public class Attributes 
{
   public int ObjectId {get; set;}
   public string Schcd {get; set;}
   public string Schnm {get; set;}
}

public class Geometry
{
   public double X {get; set;}
   public double Y {get; set;}
}

Then you just use JsonConverter (JSON.Net) like this:

MyCoolClass tmp = JsonConvert.DeserializeObject<MyCoolClass>(jsonStringThatYouGot);
Community
  • 1
  • 1
Alex
  • 3,689
  • 1
  • 21
  • 32
0

To parse a JSON you will most likely need a extern library like JSON.Net or something similar.

Then you need to create a classes (a wrapper) with properties that match your JSON String and deserialize the string into that object.

When you create the wrapper class into which you want to deserialize your JSON String you have be careful to name the properties the same way they are named in the JSON String.

Example:

public class MyObject {
  public List <Feature> Features {get;set;} 
}

public class Feature {
  public MyAttributes Attributes {get;set; }
  public Geometries Geometries {get;set; }
}

public class MyAttributes {
  public int ObjectID {get;set;}
  public string Schcd {get;set;}
  public string Schnm {get;set;}
}

public class Geometries {
  public double X {get;set;}
  public double Y {get;set;}
}

Then just call the deserialisation function of the JSON library:

var myObject = JsonConvert.DeserializeObject<MyObject>(jsonString);

To access a property like the Object ID then you just call:

myObject.Features[0].Attributes.ObjectId;
Bojan B
  • 2,091
  • 4
  • 18
  • 26
  • thanks Bojan ,I have upvoted your answer thank you again,but I searching to get the GEOMETY class X,Y values with a objetct reference of MyObject? – user3728743 Aug 27 '16 at 09:40
  • var v = myObject.features[0].Attributes.schcd; Show me error like Cannot apply indexing with [] to an expression of type system.Collection.Generic.IEnumerable<>.Any suggestions please. – user3728743 Aug 27 '16 at 10:25
  • try `myObject.Features.ToList()[0].Attributes.schcd` since the deserialization converted the List of features to an IEnumerable you need to call the ToList() function to access with [] operator... did you set the Features Property in your wrapper class as `List` or `IEnumerable` ? – Bojan B Aug 27 '16 at 11:09
  • Thanks thanks a lot bhojan you have saved my day, – user3728743 Aug 27 '16 at 11:28
  • No problem :), btw if u think others can benfit from this, you can accept the answer ;) – Bojan B Aug 27 '16 at 11:31
0

If you're ok with using dynamic you can do like this

var json = "{ \"features\" : [ { \"attributes\": { \"OBJECTID\": 1, \"schcd\": \"29030300431\", \"schnm\": \"UNAIDED GENITALIA LIPS DALMATIA RS\" }, \"geometry\":  { \"x\": 8449476.63052563, \"y\": 1845072.4204768054 } } ]  }";

dynamic obj = JsonConvert.DeserializeObject(json);
var objectId = obj.features[0].attributes.OBJECTID;

It uses Newtonsoft.Json, which can be installed with Nuget from here.

Install-Package Newtonsoft.Json

Be aware that this doesn't do any null-checks, and simply assumes that it's the first object in features that you want. But it's simple and doesn't require you to create any models.

If you want to use a more complete model I would go for the answer provided by Bojan B or j0ey_wh.

Community
  • 1
  • 1
smoksnes
  • 10,509
  • 4
  • 49
  • 74