没有Json反序列化的结果

时间:2015-04-02 19:49:53

标签: c# json

我从zoho获得了json。我有一个类似以下的JSON:

{
  "response": {
    "result": {
      "Leads": {
        "row": [
          {
            "no": "1",
            "FL": [
              {
                "content": "1325469000000679001",
                "val": "LEADID"
              },
              {
                "content": "1325469000000075001",
                "val": "SMOWNERID"
              },
              {
                "content": "Geoff",
                "val": "Lead Owner"
              },
            ]
          },
          {
            "no": "2",
            "FL": [
              {
                "content": "1325469000000659017",
                "val": "LEADID"
              },
              {
                "content": "1325469000000075001",
                "val": "SMOWNERID"
              },
              {
                "content": "Geoff",
                "val": "Lead Owner"
              },
            ]
          },

        ]
      }
    },
    "uri": "/crm/private/json/Leads/getRecords"
  }
}

我使用以下课程:

public class Row
{

    [JsonProperty(PropertyName = "row")]
    public List<Leads> row { get; set; }

}

public class Leads
{
    [JsonProperty(PropertyName = "no")]
    public string nbr { get; set; }

    [JsonProperty(PropertyName = "FL")]
    public List<Lead> FieldValues { get; set; }

}

public class Lead
{

    [JsonProperty(PropertyName = "content")]
    public string Content { get; set; }

    [JsonProperty(PropertyName = "val")]
    public string Val { get; set; }

}

我尝试反序列化json并且什么都不回来:

var mList = JsonConvert.DeserializeObject<IDictionary<string, Row>>(result);

这是第一次与Json合作,所以任何帮助都会受到赞赏!

1 个答案:

答案 0 :(得分:3)

通常在发生这种情况时,因为反序列化的类模型是错误的。而不是试图手工制作我喜欢使用http://json2csharp.com的类。只需插入您的JSON,它就会为您提供必要的C#类。在您的情况下,它提供以下内容。

public class FL
{
    public string content { get; set; }
    public string val { get; set; }
}

public class Row
{
    public string no { get; set; }
    public List<FL> FL { get; set; }
}

public class Leads
{
    public List<Row> row { get; set; }
}

public class Result
{
    public Leads Leads { get; set; }
}

public class Response
{
    public Result result { get; set; }
    public string uri { get; set; }
}

public class RootObject
{
    public Response response { get; set; }
}

然后可以使用以下命令反序列化为RootObject:

var mList = JsonConvert.DeserializeObject<RootObject>(result);

随意将RootObject重命名为您喜欢的任何名称。