Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using System.Web.Script.Serialization.JavascriptSerializer to deserialize JSON - how to?

Note: I posted a similar question, which was the ancestor of this question, as I was originally thinking of using JSON.NET to parse the JSON, but I'm using the built-in deserializer, so it's a different question.

This is what I'm trying to do: I have a class called Item, for example. The json has many "elements" (if that's what they are called - they mimic the Item class), and each contains 3 fields: an integer named id, a string named name, and a datetime named creationTime. I would like to parse all of these Item "elements" from the json into a list of Item objects. I have created 3 fields in the Item class to match the JSON.

This is what I'm currently doing:

JavaScriptSerializer ser = new JavaScriptSerializer();          
List<Item> items = ser.Deserialize<Item>(new StreamReader(response.GetResponseStream()).ReadToEnd());

However, this isn't working, because I "cannot implicitly convert type 'superapi.Item' to 'System.Collections.Generic.List<superapi.Item>'". Thus, I do not know how to approach this problem, as there are many elements of the Item architecture in the JSON. Is it somehow possible to do this in a foreach loop, to find each deserialized Item in the JSON, add that to the list, and continue the loop? I'm going to attempt to do just that with some similar code, and I will post my results.

Thanks!

UPDATE: This is what some of the JSON looks like:

[{
    "Id": 1,
    "Name": "First item name",
    "creationTime": "\/Date(1247258293690)\/"
},
{
    "Id": 2,
    "Name": "Second item name",
    "creationTime": "\/Date(1247088323430)\/"
}]

This is what my Item class looks like:

public class Item
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime creationTime { get; set; }
    }
like image 783
Maxim Zaslavsky Avatar asked Sep 20 '09 07:09

Maxim Zaslavsky


People also ask

How to deserialize a string into an object using javascriptserializer?

The JavaScriptSerializer.Deserialize<T> (input) method attempts to deserialize a string of valid JSON into an object of the specified type <T>, using the default mappings natively supported by JavaScriptSerializer.

How do I serialize a JSON object in JavaScript?

To serialize an object, use the Serialize method. To deserialize a JSON string, use the Deserialize or DeserializeObject methods. To serialize and deserialize types that are not natively supported by JavaScriptSerializer, implement custom converters by using the JavaScriptConverter class.

What is the javascriptserializer class in NewtonSoft JSON?

For earlier versions of .NET Framework, use Newtonsoft.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. You cannot access that instance of the serializer. However, this class exposes a public API.

What is the javascriptserializer class used for?

For earlier versions of .NET Framework, use Newtonsoft.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. You cannot access that instance of the serializer.


1 Answers

I tried with your example json / class, and the following works fine:

List<Item> items = ser.Deserialize<List<Item>>(json);

Is the actual code any different?

(where json is the string - feel free to replace by ReadToEnd etc; or use WebClient.DownloadString, which is simpler)

like image 94
Marc Gravell Avatar answered Sep 28 '22 06:09

Marc Gravell