C# 使用 System.Web.Script.Serialization.JavascriptSerializer 反序列化 JSON - 如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1450513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using System.Web.Script.Serialization.JavascriptSerializer to deserialize JSON - how to?
提问by Maxim Zaslavsky
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.
注意:我发布了一个类似的问题,这是这个问题的祖先,因为我最初想使用 JSON.NET 来解析 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.
这就是我想要做的:例如,我有一个名为 Item 的类。json 有许多“元素”(如果这就是它们的名字的话——它们模仿 Item 类),每个都包含 3 个字段:一个名为 id 的整数、一个名为 name 的字符串和一个名为 creationTime 的日期时间。我想将 json 中的所有这些 Item“元素”解析为 Item 对象列表。我在 Item 类中创建了 3 个字段来匹配 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.
但是,这不起作用,因为我“无法将类型 'superapi.Item' 隐式转换为 'System.Collections.Generic.List <superapi.Item
>'”。因此,我不知道如何解决这个问题,因为 JSON 中有很多 Item 架构的元素。是否有可能在 foreach 循环中执行此操作,在 JSON 中找到每个反序列化的 Item,将其添加到列表中,然后继续循环?我将尝试用一些类似的代码来做到这一点,我会发布我的结果。
Thanks!
谢谢!
UPDATE:This is what some of the JSON looks like:
更新:这是一些 JSON 的样子:
[{
"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:
这是我的 Item 类的样子:
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime creationTime { get; set; }
}
采纳答案by Marc Gravell
I tried with your example json / class, and the following works fine:
我尝试使用您的示例 json/class,以下工作正常:
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)
(json
字符串在哪里- 随意替换为ReadToEnd
etc;或者使用WebClient.DownloadString
,这更简单)