将Json序列化为c#类抛出错误

时间:2016-09-30 06:33:38

标签: c# json

我有一个像这样的网页返回的JSON

{
  "data": {
    "normal_customer": {
      "0": {
        "id": 1,
        "name": "ALPHY"


        }
      },
      "1": {
        "id": 2,
        "name": "STEVEN"          

      }

    },
    "luxury_customer": {
      "3": {
        "id": 8,
        "name": "DEV"

       }
    }
  }
}

我创建了c#classes

public class StandardCustomers
{
    public List<CustomersDetails> Customers_Details { get; set; }
}

public class CustomersDetails
{
    [JsonProperty("id")]
    public int id { get; set; }

    [JsonProperty("name")]
    public string name { get; set; }

    public class LuxuryCustomers
    {
        public List<CustomersDetails> Customers_Details { get; set; }
    }

    public class Data
    {
        public StandardCustomers standard_Customers { get; set; }
        public LuxuryCustomers luxury_Customers { get; set; }
    }

    public class RootObject
    {
        public Data data { get; set; }
    }
}

当我使用下面的c#代码

反序列化来自网站的响应时
var result1 = JsonConvert.DeserializeObject<Data>(response);

但result1.luxury_customers包含customerdetails,为null。

正如@hellostone所建议的,我已经修改为rootdata,然后也是 result1.luxury_customers包含customerdetails为null。

知道如何反序列化为c#class

当我们将Json粘贴到visual studio时,它会生成以下类

 public class Rootobject
    {
        public Data data { get; set; }
    }

    public class Data
    {
        public Standard_Customers standard_Customers { get; set; }
        public Luxury_Customers luxury_Customers { get; set; }
    }

    public class Standard_Customers
    {
        public _0 _0 { get; set; }
        public _1 _1 { get; set; }
        public _2 _2 { get; set; }
        public _4 _4 { get; set; }
        public _5 _5 { get; set; }
    }

    public class _0
    {
        public int id { get; set; }
        public string name { get; set; }

    }

个别类是在标准客户中生成的,我们可以使用列表吗

2 个答案:

答案 0 :(得分:1)

我想问题是 time echo -e "2\n'3 4 5 6 7'\n3" | python sample.py 中的索引不是从零开始的。尝试使用LuxuryCustomers中的luxury_customers代替Dictionary<string,CustomersDetails>

我已经设法用这个类反序列化Json:

List<CustomersDetails>

反序列化代码:

public class CustomersDetails
{
    [JsonProperty("id")]
    public int id { get; set; }
    [JsonProperty("name")]
    public string name { get; set; }
}

public class Data
{
    public Dictionary<string, CustomersDetails> normal_customer { get; set; }
    public Dictionary<string,CustomersDetails> luxury_customer { get; set; }
}

public class RootObject
{
    public Data data { get; set; }
}

P.S。我在“ALPHY”元素之后删除了一个结束括号,以使Json有效,我希望这是错字并且你得到了有效的Json。

答案 1 :(得分:0)

您的json字符串与您定义的类不匹配。如果要保留类结构,则Json字符串应如下所示:

{
"data":{
  "standard_Customers":{
     "Customers_Details":[
        {
           "id":1,
           "name":"ALPHY"
        },
        {
           "id":2,
           "name":"STEVEN"
        }
     ]
  },
  "luxury_Customers":{
     "Customers_Details":[
        {
           "id":8,
           "name":"DEV"
        }
     ]
  }
 }
}

注意&#34; Customers_Details&#34;中的方括号属性。

然后是:

var result1 = JsonConvert.DeserializeObject<RootObject>(response);
在使用你的班级结构时,

调用,会给你正确的对象,它不应该是null

public class StandardCustomers
{
    public List<CustomersDetails> Customers_Details { get; set; }
}

public class CustomersDetails
{
   [JsonProperty("id")]
   public int id { get; set; }
   [JsonProperty("name")]
   public string name { get; set; }
}

public class LuxuryCustomers
{
   public List<CustomersDetails> Customers_Details { get; set; }
}

public class Data
{
    public StandardCustomers standard_Customers { get; set; }
    public LuxuryCustomers luxury_Customers { get; set; }
}

public class RootObject
{
   public Data data { get; set; }
}