添加多个项目在JSON数组使用Json.net在C#对象 [英] Add multiple items in JSON array to object in C# using Json.net

查看:349
本文介绍了添加多个项目在JSON数组使用Json.net在C#对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我怎么可以反序列化包含多个属性的对象?



下面给出的情况下,代码工作正常。

 公众的ActionResult指数()
{
串JSON = @{名:人2, 电子邮件:example@example.com};

VAR EMP = JsonConvert.DeserializeObject<&人GT;(JSON);
的Response.Write(emp.name + emp.email);
返回查看();
}

公共类Person
{
公共字符串名称{;组; }
公共字符串电子邮件{获得;组; }
}



但我必须做的,如果该数组包含多个项目,例如:

 字符串JSON = @{数据:[{名:人1 ,电子邮件:test@test.com},{名:人2,电子邮件:example@example.com}]}  

在此先感谢



这些问题的答案已经下面给出的是完美的,我问这个问题,但现在我已经领先了一步。任何人都可以提出建议什么我需要做的,如果在JSON它有一个数组例如增加一个地址,在



  {
数据?:[
{
名:人1,
电子邮件:test@test.com,
地址:{
地址1:我的地址1,
地址2:我的地址2
}
},
{
名:人2,
电子邮件:例如@ example.com,
地址:{
地址1:我的地址1,
地址2:我的地址2
}
}
]
}


解决方案

这样的事情在过去一直为我工作:

  JObject O = JObject.Parse(JSON); //这将是你在上面
定义的字符串//下一行应该产生一个List<>的Person对象...
名单,LT;人>列表= JsonConvert.DeserializeObject<名单,LT;人>>(O [数据]的ToString());

您可能需要来装饰你的Person对象如下:

  [JSONObject的(MemberSerialization.OptIn)
公共类Person
{
[JsonProperty]
公共字符串名称{ ;集;}
[JsonProperty]
公共字符串电子邮件{获得;设置;}
}


Can anyone tell me how I can deserialize an object that contains multiple attributes?

Given the scenario below, the code works fine.

public ActionResult Index()
{
    string json = @"{""name"": ""Person 2"",""email"": ""example@example.com""}";

    var emp = JsonConvert.DeserializeObject<Person>(json);
    Response.Write(emp.name + emp.email);
    return View();
}

public class Person
{
    public string name { get; set; }
    public string email { get; set; }
}

But what do I have to do if the array contains multiple items, e.g

string json = @"{""data"": [{""name"": ""Person 1"",""email"": ""test@test.com""},{""name"": ""Person 2"",""email"": ""example@example.com""}]}";

Thanks in advance

The answers already given below were perfect for the problem I asked, but now I've gone one step ahead. Can anyone advise on what I'd need to do if the json had an array in it e.g. the addition of an address in?

{
    "data": [
        {
            "name": "Person 1",
            "email": "test@test.com",
            "address": {
                "address1": "my address 1",
                "address2": "my address 2" 
            } 
        },
        {
            "name": "Person 2",
            "email": "example@example.com",
            "address": {
                "address1": "my address 1",
                "address2": "my address 2" 
            } 
        } 
    ] 
}

解决方案

Something like this has worked for me in the past:

JObject o = JObject.Parse(json); // This would be the string you defined above
// The next line should yield a List<> of Person objects...
List<Person> list = JsonConvert.DeserializeObject<List<Person>>(o["data"].ToString());

You might want to decorate your Person object as follows:

[JsonObject(MemberSerialization.OptIn)]
public class Person
{
    [JsonProperty]
    public string name{get;set;}
    [JsonProperty]
    public string email{get;set;}
}

这篇关于添加多个项目在JSON数组使用Json.net在C#对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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