Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing a JSON file with JavaScriptSerializer()

the json file's structure which I will deserialize looks like below;

{     "id" : "1lad07",     "text" : "test",     "url" : "http:\/\/twitpic.com\/1lacuz",     "width" : 220,     "height" : 84,     "size" : 8722,     "type" : "png",     "timestamp" : "Wed, 05 May 2010 16:11:48 +0000",     "user" : {         "id" : 12345,         "screen_name" : "twitpicuser"     } } 

I have created a class which has the filed names as properties for JavaScriptSerializer. The code which I will use to Deserialize the json is as follows;

            using (var reader = new StreamReader(twitpicResponse.GetResponseStream())) {                   var responseBody = reader.ReadToEnd();                 var deserializer = new JavaScriptSerializer();                 var results = deserializer.Deserialize<Response>(responseBody);              } 

My problem is how I can read the user field on json file. which is like below;

"user" : {     "id" : 12345,     "screen_name" : "twitpicuser" } 

it has sub properties and values. how can I name them on my Response class. my response class now look like this;

public class Response {      public string id { get; set; }     public string text { get; set; }     public string url { get; set; }     public string width { get; set; }     public string height { get; set; }     public string size { get; set; }     public string type { get; set; }     public string timestamp { get; set; }  } 

what is the best case to do it?

like image 856
tugberk Avatar asked Mar 31 '11 15:03

tugberk


People also ask

How do I deserialize JSON?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

How do you deserialize a JSON object in Python?

The default method of deserialization is json. loads() which takes a string as an input and outputs a JSON dictionary object. To convert the dictionary object to a custom class object, you need to write a deserialize method. The easiest way is to add a static method to the class itself.

How do I deserialize JSON in Apex?

deserialize() , you must specify the type of value you expect the JSON to yield, and Apex will attempt to deserialize to that type. JSON. serialize() accepts both Apex collections and objects, in any combination that's convertible to legal JSON. String jsonString = JSON.

What is the use of JavaScriptSerializer in C#?

Json. The JavaScriptSerializer class is used internally by the asynchronous communication layer to serialize and deserialize the data that is passed between the browser and the Web server.


1 Answers

  1. You need to create a class that holds the user values, just like the response class User.
  2. Add a property to the Response class 'user' with the type of the new class for the user values User.

    public class Response {      public string id { get; set; }     public string text { get; set; }     public string url { get; set; }     public string width { get; set; }     public string height { get; set; }     public string size { get; set; }     public string type { get; set; }     public string timestamp { get; set; }     public User user { get; set; }  }  public class User {      public int id { get; set; }     public string screen_name { get; set; }  } 

In general you should make sure the property types of the json and your CLR classes match up. It seems that the structure that you're trying to deserialize contains multiple number values (most likely int). I'm not sure if the JavaScriptSerializer is able to deserialize numbers into string fields automatically, but you should try to match your CLR type as close to the actual data as possible anyway.

like image 103
ntziolis Avatar answered Oct 09 '22 03:10

ntziolis