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

1

7

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; }
    }
Fusion answered 20/9, 2009 at 7:26 Comment(9)
Any chance of a simplified block of your example problem - a simplified class and simplified example json?Youth
@Marc um, okay... im going to try to do that... by class do you mean the Item class too?Fusion
@Marc done! i have inserted sample JSON and class code... whoopee!Fusion
@Marc wow, thank you! The question that I posted yesterday about doing something similar, but with JSON.NET hasn't gotten 1 single answer whatsoever... thank you for making my day!Fusion
The site is a little quieter on the weekend...Youth
Re comments: which attributes do you have?Youth
@Marc, none really, but I believe that I saw somewhere (in my quest for finding how to make this work) that you can add an [XMLAtribute] thing over each property in the class - I think that was what it was called.Fusion
Hmmm... well, assuming the names in the JSON are all correct I suspect you're going to have to identify the problem the hard way - start with something like the simple example (that works) and increase the complexity until it breaks. Then focus on that. (btw, I'm heading off in a second - not ignoring you, just afk...)Youth
@Marc so, should I just add in the names from the JSON one by one until it stops working? I'm gonna try that in the morning! Thanks!Fusion
Y
8

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)

Youth answered 20/9, 2009 at 7:48 Comment(5)
Oh, duh! I've been setting the T of ser.Deserialize to just Item, but it's List<Item>! Stupid me! ;) going to try it now. thanks so much!Fusion
@Marc Hmm, it's giving me an InvalidOperationException on the Deserialize call, saying that it can't convert null to a value type... im going to try to look further into what's going on.Fusion
@Marc what i think is happening is that it's not recognizing the attributes in my Item class, and their relation to the JSON. Do they need any special attributes?Fusion
@Marc by "is the actual code any different", do you mean that my Item class and the JSON was a bit generalized and simplified? To that, the answer is yes. But it should still work, because the JSON seems to be structually sound.Fusion
@Marc What the actual code is, is a simple .NET library for the pre-alpha SO Api. I just saw it on meta, and was looking for an opportunity to play around with some json in a nice way, so I started building one. Don't worry, I'm not abusing it, because I <3 StackOverflow! The JSON I'm really using is at: stackoverflow.com/api/…Fusion

© 2022 - 2024 — McMap. All rights reserved.