-4

我有这样的 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、estimate、city 等的值...

那么如何通过名称访问这些元素呢?

4

1 回答 1

2

仔细查看您的 json 输入(并假设您使用的是标签指示的 C#),前提是我正确理解您的问题。您可以执行以下操作:

创建一些您可以映射到的对象,在VS2012 Update 2中,您可以复制您在问题中提供的 JSON -> 打开 .cs 文件 -> 右键单击​​ -> 粘贴特殊 -> "Paste JSON as Classes",其中将生成以下内容:

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等。

我希望这有帮助。

于 2013-05-28T04:06:56.387 回答