使用System.Web.Script.Serialization.JavascriptSerializer反序列化JSON-如何? [英] Using System.Web.Script.Serialization.JavascriptSerializer to deserialize JSON - how to?

查看:489
本文介绍了使用System.Web.Script.Serialization.JavascriptSerializer反序列化JSON-如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我发布了类似的问题,这个问题的源头,因为我最初考虑使用JSON.NET解析JSON,但是我使用的是内置的反序列化器,所以这是一个不同的问题.

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.

这就是我想要做的:例如,我有一个名为Item的类. json有许多元素"(如果它们就是它们的名字-它们模仿Item类),每个元素都包含3个字段:一个名为id的整数,一个名为name的字符串和一个名为creationTime的日期时间.我想将所有这些来自json的Item元素"解析为Item对象的列表.我在Item类中创建了3个字段以匹配JSON.

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.

这是我目前正在做的:

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

但是,这不起作用,因为我无法将类型'superapi.Item'隐式转换为'System.Collections.Generic.List >'".因此,我不知道如何解决此问题,因为JSON中有很多Item体系结构的元素.是否可以在foreach循环中执行此操作,以找到JSON中每个反序列化的项,然后将其添加到列表中,然后继续循环?我将尝试使用一些类似的代码来做到这一点,并将发布结果.

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.

谢谢!

更新:这是一些JSON的样子:

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)\/"
}]

这是我的Item类的样子:

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; }
    }

推荐答案

我尝试了您的示例json/class,以下操作正常:

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?

(其中json是字符串-可以随时替换为ReadToEnd等;或使用更简单的WebClient.DownloadString)

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

这篇关于使用System.Web.Script.Serialization.JavascriptSerializer反序列化JSON-如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆