1

I am using thirparty service to give me coordinates, below is the response I want to read this using c# .net in some kind of object so that I can use the information but confused how to achieve this..

{"found": 1, "bounds": [[52.45401, -1.96211], [52.45401, -1.96211]], "features": [{"id": 65140,"centroid": {"type":"POINT","coordinates":[52.45401, -1.96211]},"bounds": [[52.45401, -1.96211], [52.45401, -1.96211]],"properties": {"name": "B17 0SL"},"type": "Feature"}], "type": "FeatureCollection", "crs": {"type": "EPSG", "properties": {"code": 4326, "coordinate_order": [0, 1]}}}

Thanks

Shax
  • 4,207
  • 10
  • 46
  • 62
  • possible duplicate of [Best way to parse JSON data into a ASP.net object?](http://stackoverflow.com/questions/423294/best-way-to-parse-json-data-into-a-asp-net-object) – Tim Rogers Mar 29 '12 at 13:46
  • @BalaR this is not a nice answer......:) you could have helped more :) – Royi Namir Mar 29 '12 at 13:46
  • This will help you! http://stackoverflow.com/questions/1474377/json-library-for-c-sharp –  Mar 29 '12 at 13:47
  • @RoyiNamir It's hard to put in any effort when you know the OP didn't even try to google to see what his options are. – Bala R Mar 29 '12 at 14:05

5 Answers5

2

have a look at Newtonsoft.Json its a package that will deserialize the Json into a class for you.

but you will need to create the class structure you want to use.

Qpirate
  • 2,078
  • 1
  • 29
  • 41
  • how can i create a class structure, I mean I dont know how this organized i am totally new to this. – Shax Mar 29 '12 at 15:01
  • 1
    @Shax i have found this project on Codeplex http://jsonclassgenerator.codeplex.com/ its an app that will take the string and give you the class structure you can deserialise it to – Qpirate Mar 29 '12 at 15:24
2

Use a json parser like DataContractJsonSerializer or JavaScriptSerializer

For your case for ex., using Json.Net & dynamic keyword, you can write

dynamic jObj = JsonConvert.DeserializeObject(jsonstr);
Console.WriteLine(jObj.found);
Console.WriteLine(jObj.features[0].bounds[0][0]);
L.B
  • 114,136
  • 19
  • 178
  • 224
1

You can use JsonTextReader. The following code segment may be useful, if you are not using JSON.NET

jsonString = {"found": 1, "bounds": [[52.45401, -1.96211], [52.45401, -1.96211]], "features": [{"id": 65140,"centroid": {"type":"POINT","coordinates":[52.45401, -1.96211]},"bounds": [[52.45401, -1.96211], [52.45401, -1.96211]],"properties": {"name": "B17 0SL"},"type": "Feature"}], "type": "FeatureCollection", "crs": {"type": "EPSG", "properties": {"code": 4326, "coordinate_order": [0, 1]}}};
JsonTextReader reader = new JsonTextReader(new StringReader(jsonString));

while (reader.Read())
{
   if (reader.Value != null)
   {
      // Read Json values here
      // reader.Path               -> Gives you the key part.
      // reader.Value.ToString()   -> Gives you the value part
   }
}
Firnas
  • 1,665
  • 4
  • 21
  • 31
1

You can read the JSON like this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("URL");

            JArray array = new JArray();
            using (var twitpicResponse = (HttpWebResponse)request.GetResponse())
            {

                using (var reader = new StreamReader(twitpicResponse.GetResponseStream()))
                {
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    var objText = reader.ReadToEnd();

                    JObject joResponse = JObject.Parse(objText);
                    JObject result = (JObject)joResponse["result"];
                    array = (JArray)result["Detail"];
                    string statu = array[0]["dlrStat"].ToString();
                }

            }
Muhammad Awais
  • 4,238
  • 1
  • 42
  • 37
0

You can use JSON.NET

ABH
  • 3,391
  • 23
  • 26