简体   繁体   English

如何将JSON对象数组反序列化为c#结构

[英]How to deserialize JSON array of objects to c# structure

I have a json string that was created from serializing an array of objects : 我有一个json字符串,它是通过序列化一个对象数组创建的:

[
    {
        "html": "foo"
    },
    {
        "html": "bar"
    }
]

How can I deserialize it to some iterable C# structure ? 如何将其反序列化为某个可迭代的C#结构? I've tried this code, but I'm getting No parameterless constructor defined for type of 'System.String'. 我已经尝试过这段代码了,但是我得到No parameterless constructor defined for type of 'System.String'. error : 错误:

string[] htmlArr = new JavaScriptSerializer().Deserialize<String[]>(html);

What I want to receive is an iterable structure to get each 'html' object. 我想要获得的是一个可迭代的结构来获取每个'html'对象。

Use a class for each JSON object. 为每个JSON对象使用一个类。 Example: 例:

public class HtmlItem
{
   [DataMember(Name = "html")]
   public string Html { get; set; }
}

JavaScriptSerializer ser = new JavaScriptSerializer();          

// Serialize
string html = ser.Serialize(new List<HtmlItem> {
   new HtmlItem {  Html = "foo" },
   new HtmlItem {  Html = "bar" }
});

// Deserialize and print the html items.        
List<HtmlItem> htmlList = ser.Deserialize<List<HtmlItem>>(html);
htmlList.ForEach((item) => Console.WriteLine(item.Html)); // foo bar

You can use Newtonsoft Json.NET (available from NuGet) 您可以使用Newtonsoft Json.NET (可从NuGet获得)

string json = @"[{""html"": ""foo""},{""html"": ""bar""}]";
var items = JsonConvert.DeserializeObject<List<Item>>(json);

Where 哪里

public class Item
{
    public string Html { get; set; }
}

The docs site apparently isn't working right now... But I would try using JSON.NET ( http://james.newtonking.com/projects/json/help/ ) 文档网站显然现在不能正常工作......但我会尝试使用JSON.NET( http://james.newtonking.com/projects/json/help/

There are a couple of ways you can do it. 有几种方法可以做到。 You can deserialize in a very dynamic not type strict way or you can define an object that matches the json object exactly and deserialize into that. 您可以以非常动态的非严格方式反序列化,也可以定义一个与json对象完全匹配的对象,并反序列化为该对象。 If there are many formats of JSON you'll have to serialize I would recommend using schemas. 如果有许多格式的JSON你必须序列化我建议使用模式。

Answer of nekman is not completely correct, the attribute should be JsonPropery instead of DataMember. nekman的答案不完全正确,属性应该是JsonPropery而不是DataMember。 (in this case you can remove the attribute since the deserializer doesn't care about the capital H) (在这种情况下,您可以删除该属性,因为反序列化器不关心大写H)

public class HtmlItem
{
   [JsonProperty("html")]
   public string Html { get; set; }
}

JavaScriptSerializer ser = new JavaScriptSerializer();          

// Serialize
string html = ser.Serialize(new List<HtmlItem> {
   new HtmlItem {  Html = "foo" },
   new HtmlItem {  Html = "bar" }
});

// Deserialize and print the html items.        
List<HtmlItem> htmlList = ser.Deserialize<List<HtmlItem>>(html);
htmlList.ForEach((item) => Console.WriteLine(item.Html)); // foo bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM