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
    }
}]
}
4
  • The json looks fine. What have you tried? And why doesn't it work? Commented Aug 26, 2016 at 5:50
  • Here's an excellent example for json parsing - stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c Commented Aug 26, 2016 at 5:51
  • I want to get the value of OBJECTID i dont how,I tried some way but ,I failed Commented Aug 26, 2016 at 5:52
  • ... no one's going to talk about the genitalia in the JSON? Commented Aug 10, 2018 at 21:03

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

5 Comments

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?
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.
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 ?
Thanks thanks a lot bhojan you have saved my day,
No problem :), btw if u think others can benfit from this, you can accept the answer ;)
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.

3 Comments

Thanks for the smokes,This is the error i got 'Newtonsoft.Json.Linq.JObject' does not contain a definition for 'features'
The example in the answer works and returns objectid 1. Are you sure you are using the same json as you posted?
Also. Make sure to set it to be a dynamic.
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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.