Confusion in fetching data from JSON

I am trying to use one web service which returns demanded data in json format. Now the actual point is I can fetch the data from the particular web service url in string.

  string url= @"http://api.oodle.com/api/v2/listings?key=TEST&region=chicago&category=vehicle&format=json";

  string jsonString = new WebClient().DownloadString(url);

Now the point is I get the data in string (in JSON format). But I dont know how to convert the string into JSON string and how to fetch data from this string.

Let me give you example so you can easily understand

if my jsonString is like

{
   "current":{
      "region":{
         "id":"chicago",
         "name":"Chicago"
      },
      "category":{
         "id":"vehicle",
         "name":"Cars & Vehicles",
         "abbrev":"Vehicles"
      },
      "start":1,
      "num":10
   }
}

How can i get region_name from that string ? Hope you understand me ! Try to use Test Link !

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Add a reference to System.Web and then add to your using section

using System.Web.Script.Serialization;

Then (using your example json string)

string jsonString = "{"current":{"region":{"id":"chicago","name":"Chicago"},"category":{"id":"vehicle","name":"Cars & Vehicles","abbrev":"Vehicles"},"start":1, "num":10}}";

JavaScriptSerializer serializer = new JavaScriptSerializer();

CurrentRecord currentRecord = serializer.Deserialize<CurrentRecord>(jsonString);

string regionName = currentRecord.current.region.name;

Also add the following classes to your project:

[Serializable]
public class CurrentRecord
{
    public current current;
}

[Serializable]
public class current
{
    public region region;
    public category category;
    public int start;
    public int num;
}

[Serializable]
public class region
{
    public string id;
    public string name;
}

[Serializable]
public class category
{
    public string id;
    public string name;
    public string abbrev;
}

Method 2

Are you processing the JSON return string in Java, or JavaScript?

If you are processing the JSON response string in Java, you may make use of GSON. Here is a tutorial showing you how: Parsing a JSON String into an object with GSON easily.

For your case, you need a class like:

class Current{
  private Region region;
  private Category category;
  private int start;
  private int num;

  // getters and setters

  class Region{
    private String id;
    private String name;

    // getters and setters
  }

  class Category{
    private String id;
    private String name;
    private String abbreviation;

    // getters and setters
  }
}

Else if you are processing this JSON response String in Javascript, then you can have a look at this: http://www.json.org/js.html

alert(jsonReturnString.current.region.name);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x