开发者

How to serialize from C# to JSON a list containing a list containing an array?

开发者 https://www.devze.com 2023-03-30 10:25 出处:网络
I wish to serialize from C sharp to JSON. I would like the output to be [ [ { \"Info\": \"item1\", \"Count\": 5749 },

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
]

What would my data structure in C# look like?

Would I have something 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));
j开发者_运维知识库son[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);

? 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);

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]


This looks like a challenge because you're returning a heterogeneous array. You'll have better luck with a structure like this:

[
  { 
    "itemInfo": {
      "items": [
        { "Info": "item1", "Count": 5749 },
        { "Info": "item2", "Count": 2610 },
        { "Info": "item3", "Count": 1001 },
        { "Info": "item4", "Count": 1115 },
        { "Info": "item5", "Count": 1142 }
      ],
      "month": "June",
      "val": 37547
    },
    "day": "Monday",
    "val": 32347
  } // , { ... }, { ... }
]

This way instead of returning an array with disparate information in each slot, instead you're returning an array of well-defined objects. Then you can easily model a class that looks just like this and use a DataContractSerializer to handle the translation to JSON.


this probably wont make a difference to what you are trying to achieve but i think i should make a point of saying

public class InfoCount
{
    public InfoCount (string Info, int Count)
    {
        this.Info = Info;
        this.Count = Count;
    }
    public string Info;
    public int Count;
}

should be

public class InfoCount
{
    private string _info = "";
    private int _count = 0;
    public string Info {
            get { return _info; }
            set { _info = value; }
    }
    public int Count {
            get { return _count; }
            set { _count = value; }
    }
    public InfoCount (string Info, int Count)
    {
        this.Info = Info;
        this.Count = Count;
    }
}

well, setting the _info and _count probably are not needed.. just a good way to avoid errors.

0

精彩评论

暂无评论...
验证码 换一张
取 消