如何从C#序列化到JSON包含包含数组列表的列表? [英] How to serialize from C# to JSON a list containing a list containing an array?

查看:813
本文介绍了如何从C#序列化到JSON包含包含数组列表的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从C连载锋利JSON。我想输出为

I wish to serialize from C sharp to JSON. I would like the output to be

[
    [
        { "Info": "item1", "Count": 5749 },
        { "Info": "item2", "Count": 2610 },
        { "Info": "item3", "Count": 1001 },
        { "Info": "item4", "Count": 1115 },
        { "Info": "item5", "Count": 1142 },
        "June",
        37547
    ],
    "Monday",
    32347
]

将在C#我的数据结构是什么样子的?

What would my data structure in C# look like?

我是否有这样的事情

public class InfoCount
{
    public InfoCount (string Info, int Count)
    {
        this.Info = Info;
        this.Count = Count;
    }
    public string Info;
    public int Count;
}
List<object> json = new List<object>();
json[0] = new List<object>();
json[0].Add(new InfoCount("item1", 5749));
json[0].Add(new InfoCount("item2", 2610));
json[0].Add(new InfoCount("item3", 1001));
json[0].Add(new InfoCount("item4", 1115));
json[0].Add(new InfoCount("item5", 1142));
json[0].Add("June");
json[0].Add(37547);
json.Add("Monday");
json.Add(32347);



?我使用.NET 4.0。

? I am using .NET 4.0.

推荐答案

我会尝试使用匿名类型。

I would try using anonymous types.

var objs = new Object[]
{
    new Object[]
    {
        new { Info = "item1", Count = 5749 },
        new { Info = "item2", Count = 2610 },
        new { Info = "item3", Count = 1001 },
        new { Info = "item4", Count = 1115 },
        new { Info = "item5", Count = 1142 },
        "June",
        37547
    },
    "Monday",
    32347
};

String json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(objs);



变量 JSON 现在包含以下内容:

The variable json now contains the following:

[[{"Info":"item1","Count":5749},{"Info":"item2","Count":2610},{"Info":"item3","Count":1001},{"Info":"item4","Count":1115},{"Info":"item5","Count":1142},"June",37547],"Monday",32347]

这篇关于如何从C#序列化到JSON包含包含数组列表的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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