2013-05-28 20 views
-4

的價值我有一個JSON導致這樣JSON得到它的名字屬性

{ 
     "responseHeader": { 
      "status": 0, 
      "QTime": 1, 
      "params": { 
       "q": "zip:15241", 
       "wt": "json", 
       "fq": "propertyType:AUCTION" 
      } 
     }, 
     "response": { 
      "numFound": 2, 
      "start": 0, 
      "docs": [{ 
       "streetAddress": "1014 TALL TREES DR", 
       "estimate": 506672.0, 
       "city": "PITTSBURGH", 
       "beds": 4.0, 
       "baths": 3.5, 
       "propertyType": "AUCTION", 
       "status": "OPEN", 
       "propertyId": 778526, 
       "amountField": "OB", 
       "amount": "88888.0", 
       "enteredDate": 20101221, 
       "bed_bath": "4B 3.50BT", 
       "hasPhoto": false, 
       "auctionDate": "2012-08-07T18:30:00Z", 
       "displayAddress": "TALL TREES DR", 
       "zip": "15241", 
       "residenceType": "SFR", 
       "sqFeet": 3275.0, 
       "fcStatusName": "NTS", 
       "county": "Allegheny", 
       "state": "PA", 
       "_version_": 1429451140939907072 
      }, { 
       "streetAddress": "2567 ROSSMOOR DR", 
       "estimate": 503195.0, 
       "city": "PITTSBURGH", 
       "beds": 6.0, 
       "baths": 2.0, 
       "propertyType": "AUCTION", 
       "status": "OPEN", 
       "propertyId": 1662435, 
       "amountField": "MV", 
       "amount": "503195.0", 
       "enteredDate": 20101221, 
       "bed_bath": "6B 2BT", 
       "hasPhoto": false, 
       "auctionDate": "2010-12-24T18:30:00Z", 
       "displayAddress": "ROSSMOOR DR", 
       "zip": "15241", 
       "residenceType": "SFR", 
       "sqFeet": 6143.0, 
       "fcStatusName": "NTS", 
       "county": "Allegheny", 
       "state": "PA", 
       "_version_": 1429451149353680896 
      }] 
     } 
    } 

我想要得到的對象的列表,每個對象將包含的StreetAddress,估計,城市等的價值..

那麼如何通過名稱訪問這些元素?

+0

您的評論被標記爲「C#」,但是您沒有說出使用哪個.NET庫來解析JSON。 –

+0

喬懷特所說的背馱式,你有沒有建立你的對象序列化成? –

+3

http://james.newtonking.com/projects/json-net.aspx – Blorgbeard

回答

2

那麼看着你的JSON輸入(並假設您正在使用您的代碼表示C#),也提供了,我明白你的問題是正確的。 你可以做到以下幾點:

創建一些對象,你可以映射到,在VS2012 Update 2可以複製,你在你的問題中提供的JSON - >打開cs文件 - >右鍵 - >粘貼特殊 - >「粘貼JSON作爲類」,這將產生如下:

public class Rootobject 
{ 
    public Responseheader responseHeader { get; set; } 
    public Response  response  { get; set; } 
} 

public class Responseheader 
{ 
    public int status { get; set; } 
    public int QTime { get; set; } 
    public Params _params { get; set; } 
} 

public class Params 
{ 
    public string q { get; set; } 
    public string wt { get; set; } 
    public string fq { get; set; } 
} 

public class Response 
{ 
    public int numFound { get; set; } 
    public int start { get; set; } 
    public Doc[] docs  { get; set; } 
} 

public class Doc 
{ 
    public string streetAddress { get; set; } 
    public float estimate  { get; set; } 
    public string city   { get; set; } 
    // etc ... 
} 

那麼你可以使用Json.NET(你也可以從NuGet得到)的輸入反序列化到一個適當的C#對象,如這個:

Rootobject rootObject = JsonConvert.DeserializeObject<Rootobject>(jsonInput); 

當然,您現在可以訪問任何它的性能,如 - >rootObject.response.docs

我希望這有助於。

+1

感謝您的回答。 –

+0

@ArulDinesh :)你知道了,夥計 –