Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion in fetching data from JSON

Tags:

json

c#

asp.net

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 !

like image 781
Chintan Avatar asked Feb 02 '12 09:02

Chintan


2 Answers

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;
}
like image 168
Nicholas Murray Avatar answered Nov 06 '22 15:11

Nicholas Murray


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);
like image 37
Oh Chin Boon Avatar answered Nov 06 '22 13:11

Oh Chin Boon