在c#中使用多个数组创建json对象

时间:2017-07-25 13:00:14

标签: c# json

我想用以下格式创建JSON对象: -

{
  "result": [
      {
        "name": "John",
        "address": "US",
      },
      {
        "name": "Josh",
        "address": "Japan",
      }
  ],
  "error": [
    {
      "message": "error-message"
    }
  ],
  "success": [
    {
      "message": "success-message"
    }
  ]
}

我尝试了以下内容,但它对我没有帮助。

dynamic record = new { result = new {name="", address=""},
                   error = new {message=""},
                   success = new {message=""} };

更新1: -

这是我的代码: -         列表addressList = new List();

  // Loop over items within the container and URI.
    foreach (var item in items)
    {
        dynamic record = new { result = new object[] {
    new {name = item.name, address = item.address} } };
        addressList.Add(record);

    }

结果: -

[  {
    "result": [
      {
        "name": "John",
        "address": "US"
      }
    ]
  },
  {
    "result": [
      {
        "name": "Jack",
        "address": "CA"
      }
    ]
  }
]

预期的json结果: -

  [{
        "result": [{
            "name": "John",
            "address": "US"
        }]
    },
    {
        "result": [{
            "name": "Jack",
            "address": "CA"
        }],
        "error": [{
            "message": "error-message"
        }],
        "success": [{
            "message": "success-message"
        }]
    }
]

如何更新我的代码以获得高于预期的json结果?

5 个答案:

答案 0 :(得分:3)

你...创建数组。你不是那样做的。您正在创建单个对象。

有些事情:

dynamic record = new {
    result = new object[] {
        new {name = "John", address = "US"},
        new {name = "Josh", address = "Japan"}
    },
    error = new object[] /*...*/,
    success = new object[] /*...*/
};

答案 1 :(得分:1)

如果你想要完全是JSON,那么 newtonsoft.Json 会让它更容易:

Json json = new Json();

json.result = new object[] {
    new {name = "John", address = "US"},
    new {name = "Josh", address = "Japan"}
};
// json.error = ... and so on

string output = JsonConvert.SerializeObject(product);

您将拥有的输出是:

{
  "result": [
    {
      "name": "John",
      "address": "US",
    },
    {
      "name": "Josh",
      "address": "Japan",
    }
  ],
  "error": [
    {
      ...
    }
  ]
}

要将其反序列化,请使用:

Json deserializedJson = JsonConvert.DeserializeObject<Json>(output);

答案 2 :(得分:0)

你没有创建一个数组 如果你想从c#创建JSON数组,你必须使用以下POCO

public class Result
{
    public string name { get; set; }
    public string address { get; set; }
}

public class Error
{
    public string message { get; set; }
}

public class Success
{
    public string message { get; set; }
}

public class RootObject
{
    public List<Result> result { get; set; }
    public List<Error> error { get; set; }
    public List<Success> success { get; set; }
}

然后使用Json.net

var json = JsonConvert.SerializeObject( "your instance of the root Object")

答案 3 :(得分:0)

//你需要先建立这个类结构

public class Response
{
    public List<Result> result { get; set; }
    public List<Error> error { get; set; }
    public List<Success> success { get; set; }
}

public class Result
{
    public string name { get; set; }
    public string address { get; set; }
}

public class Error
{
    public string message { get; set; }
}

public class Success
{
    public string message { get; set; }
}

//然后你可以像这样使用它

        var response = new Response()
        {
            result = new List<Result>
            {
                new Result() {name = "Jhon", address = "US"},
                new Result() {name = "Jhon", address = "US"},
            },
            error = new List<Error>()
            {
                new Error() {message = "error-message 1"},
                new Error() {message = "error-message 2"}
            },
            success = new List<Success>()
            {
                new Success(){message = "success-message 1"},
                new Success(){message = "success-message 2"},
            }
        };

答案 4 :(得分:0)

模型类

public class MegaMenu
    {
        public int department_id { get; set; }
        public string department_name { get; set; }
        public List<SectionListData> sectionListData { get; set; }
    }
    public class SectionListData 
    {
        public int section_id { get; set; }
        public string section_name { get; set; }
        public List<ItemHeadList> itemHeadList { get; set; }
    }
    public class ItemHeadList
    {
        public int item_head_id { get; set; }
        public string item_name { get; set; }
    }
    public class WrapperMegaMenu
    {
        public List<MegaMenu> megaMenuList { get; set; }
        public string error { get; set; }
    }

服务

dept_result是所有部门的数组,section_result是所有部门的数组,Item_result是所有项目的数组

List<MegaMenu> listmenu = new List<MegaMenu>();
            foreach (var each_dept in dept_result)
            {
                MegaMenu megaMenu = new MegaMenu();
                megaMenu.department_id = each_dept.shopdepartment_id;
                megaMenu.department_name = each_dept.name_en;
                var temSectionList = section_result
                .Where(item => item.shopdepartment_id == each_dept.shopdepartment_id).ToList().Select(sectionData => new SectionListData
                {
                    section_id = sectionData.shopsection_id,
                    section_name = sectionData.name_en,
                    itemHeadList = Item_result.Where(itemHead => itemHead.shopsection_id == sectionData.shopsection_id).ToList().Select(itemHeadData => new ItemHeadList {
                        item_head_id = itemHeadData.item_head_id,
                        item_name = itemHeadData.name_en
                    }).ToList()
                }).ToList();
                megaMenu.sectionListData = temSectionList;
                listmenu.Add(megaMenu);
            }
            //wrapperDept.departmentList = dept_result.ToList();

            wrapper.megaMenuList = listmenu.ToList();

结果

{
    "megaMenuList": [
        {
            "department_id": 71,
            "department_name": "Baby's Hygiene",
            "sectionListData": [
                {
                    "section_id": 56,
                    "section_name": "Diapers",
                    "itemHeadList": []
                },
                {
                    "section_id": 57,
                    "section_name": "Wipes",
                    "itemHeadList": [
                        {
                            "item_head_id": 142,
                            "item_name": "Telivision"
                        }
                    ]
                }
            ]
        }
    ]
}