简体   繁体   English

序列化JSON对象数组(Json.NET)

[英]Serializing JSON Object Array (Json.NET)

I'm learning JSON and was wondering how to create an array of objects. 我正在学习JSON,想知道如何创建对象数组。 I want my JSON file to look like this 我希望我的JSON文件看起来像这样

{
    "Place": {
        "Stores": [{
            "Grocery": {
                "stock": "fruit",
                "distance": 19,
                "size": 12
            },
            "Department": {
                "stock": "clothing",
                "distance": 21,
                "size": 7
            }
        }]
    }
}

Here is what my C# classes looks like 这是我的C#类的样子

public class RootObject
{
    public Place Place { get; set; }
}

public class Place
{
    public List<Store> Stores { get; set; }
}

public class Store
{
    public Grocery Grocery { get; set; }
    public Department Department { get; set; }
}

public class Grocery
{
    public string stock { get; set; }
    public int distance { get; set; }
    public int size { get; set; }
}

public class Department
{
    public string stock { get; set; }
    public int distance { get; set; }
    public int size { get; set; }
}

So far, I have tried coding it like this, similar to how the examples are on the newtonsoft website 到目前为止,我已经尝试过像这样编码,类似于newtonsoft网站上的示例

Rootobject root = new Rootobject
{
    Place = new Place
    {
        stores = new List<Store>
        {
            Grocery = new Grocery
            {
                stock ="fruit",
                distance = 19,
                size = 12
            },
            Department = new Department
            {
                stock ="clothing",
                distance = 21,
                size = 7
            }
        }
    }
};

string json = JsonConvert.SerializeObject(root,          
    Formatting.Indented,              
    new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\output.json", json);

but I'm getting two CS0117 errors at 但我收到两个CS0117错误

Grocery = new Grocery

and

Department = new Department

which says that Store does not contain a definition for Grocery / Department 表示Store不包含Grocery / Department的定义

What am I doing wrong here? 我在这里做错了什么? Did I just make an error in syntax or is there a possibility I am just approaching serializing this the wrong way? 我只是在语法上犯了一个错误,还是有可能我正以错误的方式进行序列化? Much thanks in advance for your guy's help. 在此先感谢您的帮助。

Your object should look like this: 您的对象应如下所示:

 Rootobject root = new Rootobject
    {
        Place = new Place
        {
            stores = new List<Store>
            {
                new Store{
                   Grocery = new Grocery
                  {
                    stock ="fruit",
                    distance = 19,
                    size = 12
                  },
                 Department = new Department
                {
                    stock ="clothing",
                    distance = 21,
                    size = 7
                }
               }
            }
        }
    };

I wrote it from my head, so I hope syntax is good. 我从脑海中写下来,所以我希望语法是好的。 But main idea is that you were creating list of stores and not any store inside that list. 但是主要的想法是您要创建商店列表,而不是列表中的任何商店。 You should create some Store using new Store 您应该使用new Store创建一些new Store

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

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