如何使用restsharp读取Json数组

时间:2012-10-11 13:35:44

标签: json deserialization restsharp

我需要使用RestSharp阅读以下代码。我的问题是如何使数组在适当的结构。如何设置包含对象的类以使其正常工作?

我想在AcUserInfo类型的List中反序列化对象“0”和“1”。

非常感谢。 安德烈

{
   "0":{
      "id":"2",
      "subscriberid":"2",
      "cdate":"2012-09-28 16:49:06",
      "sdate":"2012-09-28 16:49:06",
      "first_name":"Al",
      "last_name":"",
      "email":"test@verizon.net"
   },
   "1":{
      "id":"29",
      "subscriberid":"29",
      "cdate":"2012-10-02 15:08:29",
      "sdate":"2012-10-02 15:08:29",
      "first_name":"Mark",
      "last_name":"",
      "email":"test2@verizon.net"
   },
   "result_code":1,
   "result_message":"Success: Something is returned",
   "result_output":"json"
}

这是我创建的课程:

public class SubscriberList {
    public int result_code { get; set; }
    public string result_message { get; set; }
    public string result_output { get; set; }
    public List<AcUserInfo> row { get; set; }

    SubscriberList(){
        row = new List<AcUserInfo>();
    }
}

1 个答案:

答案 0 :(得分:0)

您的JSON数据不包含数组,因此无法反序列化为List&lt;&gt;。

将您的JSON转换为以下内容:

{
  [{
  "id":"2",
  "subscriberid":"2",
  "cdate":"2012-09-28 16:49:06",
  "sdate":"2012-09-28 16:49:06",
  "first_name":"Al",
  "last_name":"",
  "email":"test@verizon.net"
  },
  {
  "id":"29",
  "subscriberid":"29",
  "cdate":"2012-10-02 15:08:29",
  "sdate":"2012-10-02 15:08:29",
  "first_name":"Mark",
  "last_name":"",
  "email":"test2@verizon.net"
  }],
  "result_code":1,
  "result_message":"Success: Something is returned",
  "result_output":"json"
}

或者如果你总是只有“0”&amp; “1”元素然后更改您的SubscriberList类以匹配它。