6

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 !

svick
  • 236,525
  • 50
  • 385
  • 514
Chintan
  • 2,768
  • 4
  • 27
  • 43

2 Answers2

7

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;
}
Nicholas Murray
  • 13,305
  • 14
  • 65
  • 84
  • 2
    Or you can use some 3rd-party JSON library, like JSON.Net. – svick Feb 06 '12 at 12:12
  • 1
    JSON.net is pretty slick, but kept with System.Web.Script.Serialization for this question. – Nicholas Murray Feb 06 '12 at 12:18
  • @NicholasMurray I have tried this ! Fully successful ! Thank you ! But I have one query , is it necessary to declare your class as `Serializable` ? Because I have tried without `Serializable` but still it works fine ! Then what is the purpose of declaring `Serializable` ? – Chintan Feb 08 '12 at 11:14
  • 1
    @NoOne - it depends what you are doing with your object - if you are passing it on to another application domain you need to mark it Serializable so it can be DeSerialized, if you are using it within the same application domain you don't. This is because objects are only valid in the application domain where they are created. http://msdn.microsoft.com/en-us/library/ms973893.aspx – Nicholas Murray Feb 08 '12 at 11:31
  • @NicholasMurray Finaly I got it! That means -> I create dll of this class -> I use this dll into another application -> If the class is not declared as `Serializable`, it will give me error (booom !) -----> If class is `Serializable`, there is no issue at all. Right na ? – Chintan Feb 08 '12 at 11:41
  • 1
    I think it means more along the lines of if you are passing the object in a stream or service to another application rather than a reference to a dll – Nicholas Murray Feb 08 '12 at 11:52
1

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);
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215