JSON获取其name属性的值

本文关键字:属性 name 获取 JSON | 更新日期: 2023-09-27 18:15:50

我有一个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等的值…

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

JSON获取其name属性的值

看看你的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等等