开发者

Can't deserialize an array of objects from JSON into a C# object

开发者 https://www.devze.com 2023-01-15 02:41 出处:网络
I\'m having trouble getting the JavascriptSerializer to deserialize an array of objects. I\'m not terribly sure what I\'m missing.

I'm having trouble getting the JavascriptSerializer to deserialize an array of objects. I'm not terribly sure what I'm missing.

Here's my object...

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


    public cla开发者_如何学Pythonss 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;
    }
}

and here's the json response I'm trying to deserialize...

     {
     "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
   }
}

What am I doing wrong?


After switching over to JSON.NET and having the same problem, I walked away for a few minutes and came back. I found my problem -- I needed to nest listing inside data, not at the same level.


You have 3 classes, yet one wrapper so you need a class that wraps the other 3 to match your root "class" level in the JSON.

0

精彩评论

暂无评论...
验证码 换一张
取 消