This project has moved. For the latest updates, please go here.

How to retrieve a value from a json string?

Apr 7, 2014 at 5:48 PM
I receive the following json string from a web request and would like to retrieve the value from the hugeJpeg name-value pair. Is there an easy way to do this. I've been looking through the documentation but not having any luck. Can anybody help me out?

"countryName":"United States",
"name":"I-95 n/o Delavan Ave",
Apr 7, 2014 at 10:46 PM
One way of doing it (I'm sure there are much cleverer ways, but...) is to create a class structure for this json "file".
An easy way of doing this is using json2csharp by Mr. Jonathan Keith.
Paste the json code and Click. Now you have the classes for handling the structure.

using the created classes you can then use this code to get hugeJpeg
 public void Test()
            string meJson = "[{'publicId':'8277','updatedAt':1393445258194," +
            "'location':{'coordinates':[-73.6467,41.008141],'cityCode':'BDR'," +
            "'cityName':'Bridgeport','stateCode':'CT','stateName':'Connecticut'," +
            "'countryCode':'US','countryName':'United States','zipCode':'06830'}," +
            "'name':'I-95 n/o Delavan Ave','content':{" +
            "'hugeJpeg':''," +
            "'halfJpeg':''," +
            "'fullJpeg':''}," +
            "'provider':'ConnDOT','orientation':'South','policy':{'isAdmin':false,'refreshRate':2000}," +

            List<RootObject> jsonlist = JsonConvert.DeserializeObject<List<RootObject>>(meJson);
            string x = jsonlist[0].content.hugeJpeg;
I don't paste the created classes since the json2csharp will create the same code every time ...

Hope this will help you!

// Anders
Apr 8, 2014 at 1:39 PM
Thank you Anders! Worked like a charm. Especially liked the Json2csharp, saved me alot of coding.
Apr 8, 2014 at 1:44 PM
Glad it worked!
If you got a static json then Json2csharp is very nice!

Otherwise you should be able to use JArray.Parse and/or JObject.Parse
But since I'm a beginner at Json.NET I haven't figured out how to use them...

// Anders