2010-09-02 205 views
3

我無法使JavascriptSerializer反序列化對象數組。我不是非常確定我錯過了什麼。無法將JSON對象數組反序列化爲C#對象

這裏是我的對象......

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

我在做什麼錯?

+0

你不應該發佈實際數據:-p – Fredou 2010-09-02 19:22:34

+0

它的公共記錄:)儘管你是絕對正確的。 – 2010-09-03 19:58:44

回答

0

你有3個類,但是隻有一個包裝器,所以你需要一個包裝其他3的類來匹配JSON中的根類「級」級別。

1

切換到JSON.NET並有同樣的問題後,我走開了幾分鐘,然後回來。我發現我的問題 - 我需要在列表中嵌入數據,而不是在同一級別。