无法将JSON中的对象数组反序列化为C#对象

时间:2010-09-02 18:55:10

标签: c# json serialization javascriptserializer

我无法让JavascriptSerializer反序列化一个对象数组。我并不十分清楚我错过了什么。

这是我的对象......

public class HomefinderResult
{
    public Data data;
    public List<Listing> listings;
    public Status status;


    public class Listing
    {
        public string recordingDate;
        public Buyer buyer;
        public double distance;
        public string id;

        public class Buyer
        {
            public Address address;
            public string phone;
            public string firstName;
            public string lastName;

            public class Address
            {
                public string line1;
                public string city;
                public string state;
                public string zip;
                public string county;
                public string latitude;
                public string longitude;

            }
        }
    }

    public class Data
    {
        public Meta meta;
        public class Meta
        {
            public int totalMatched;
            public int totalPages;
            public int currentPage;
            public double executionTime;
        }
    }

    public class Status
    {
        public int code;
        public string[] errorStack;
    }
}

这是我想要反序列化的json响应......

     {
     "data":{
        "meta":{
           "totalMatched":126,
           "totalPages":7,
           "currentPage":1,
           "executionTime":0.26153302192688
        },
        "listings":[
         {
            "recordingDate":"2010-06-07",
            "buyer":{
               "address":{
                  "line1":"999 Meadow Rd",
                  "city":"Oak Ridge",
                  "state":"TN",
                  "zip":"99999",
                  "county":"Anderson",
                  "latitude":"99.0316920000",
                  "longitude":"-99.2476320000"
               },
               "phone":"9995551234",
               "firstName":"xxx",
               "lastName":"xxx"
            },
            "distance":18.088793566409,
            "purchasePrice":"975000",
            "id":"70a706d46c5e1db7417b78c159467431"
         },

         {
            "recordingDate":"2010-06-04",
            "buyer":{
               "address":{
                  "line1":"999 Willow Ln",
                  "city":"Oak Ridge",
                  "state":"TN",
                  "zip":"99999",
                  "county":"Anderson",
                  "latitude":"99.0045700000",
                  "longitude":"-99.3182850000"
               },
               "firstName":"xxx",
               "lastName":"xxx",
               "gender":"Female"
            },
            "distance":22.244494041996,
            "purchasePrice":"201000",
            "id":"dda41ec8150c8648a7e9926b2c5fd468"
         }
      ]
   },
   "status":{
      "code":200,
      "errorStack":null
   }
}

我做错了什么?

2 个答案:

答案 0 :(得分:1)

切换到JSON.NET并遇到同样的问题后,我走了几分钟然后回来了。我发现了我的问题 - 我需要在数据中嵌套列表,而不是在同一级别。

答案 1 :(得分:0)

你有3个类,但是有一个包装器,所以你需要一个包装其他3个类的类来匹配JSON中的根“类”级别。