JSON获取其name属性的值

时间:2013-05-28 03:25:05

标签: c#

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

那么如何按名称访问这些元素?

1 个答案:

答案 0 :(得分: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);

当然,您现在可以访问任何属性,例如 - &gt; rootObject.response.docs等。

我希望这会有所帮助。