0

I've got a JSON data from Twitter API SEARCH. I'm trying to deserialize these data into objects.

The JSON scheme looks like this:

    {
      "element": INT,
      "element2": STRING,
       ..
       ..
      "Results":[
                   {
                      "user":STRING, 
                      "image":STRING,
                      ..
                      ..
                   }
                ]
   }

How could I deserialize those JSON elements into objects using JSON Toolkit or something else?

user1341970
  • 449
  • 2
  • 7
  • 15
  • Maybe this will help [http://stackoverflow.com/questions/7895105/json-deserialize-c-sharp][1] [1]: http://stackoverflow.com/questions/7895105/json-deserialize-c-sharp – Bert Rymenams Apr 20 '13 at 12:17

2 Answers2

1

Create a class that matches the JSON schema

public class Data
{
   public string Element{get;set;}
   public string Element2{get;set;}
   public List<Result> Results{get;set;}
}
public class Result
{
  public string User{get;set;}
  public string Image{get;set;}
}

and use JSON.NET to deserialize

var result = JsonConvert.DeserializeObject<Result>(json);
Jurica Smircic
  • 6,117
  • 2
  • 22
  • 27
1

If you have problems with correct type definition, you can always use dynamic deserialization using Json.Net:

var original = JsonConvert.DeserializeObject<dynamic>(jsonstring);

and then build your desired object based on it (if for example the original one contains overhead information set, and you don't need all of them):

var somepart = new {
                       E1 = original.element1,
                       E2 = original.element2
                   };
jwaliszko
  • 16,942
  • 22
  • 92
  • 158