使用jquery解析嵌套的JSON对象

时间:2016-03-04 17:21:07

标签: jquery json

我有一个嵌套的JSON对象,我在解析时遇到问题。结构如下

    {
  "status": {
    "version": "1.0.0",
    "code": 0,
    "msg": "SuccessWithResult",
    "total": 1,
    "page": 1,
    "pagesize": 10
  },
  "property": [
    {
      "identifier": {
        "obPropId": 2511639610001,
        "fips": "10001",
        "apn": "11701014062800000",
        "apnOrig": "1-17-010.14-06-28.00-000"
      },
      "lot": {
        "depth": 110,
        "frontage": 22,
        "lotnum": "28",
        "lotsize1": 600,
        "lotsize2": 2614
      },
      "area": {
        "countrysecsubd": "Kent County",
        "countyuse1": "P",
        "muncode": "17",
        "munname": "DUCK CREEK",
        "subdname": "WOODLAND MANOR PH I",
        "taxcodearea": "17"
      },
      "address": {
        "country": "US",
        "countrySubd": "DE",
        "line1": "468 SEQUOIA DR",
        "line2": "SMYRNA, DE 19977",
        "locality": "Smyrna",
        "matchCode": "ExaStr",
        "oneLine": "468 SEQUOIA DR, SMYRNA, DE 19977",
        "postal1": "19977",
        "postal2": "2542",
        "postal3": "C003"
      },
      "location": {
        "accuracy": "Street",
        "elevation": 0,
        "latitude": "39.302769",
        "longitude": "-75.594399",
        "distance": 0,
        "geoid": "MT30001363,PL1067310,RS0000330264,SD67611,SS155012,SS198222,SS201397,SS201759"
      },
      "summary": {
        "absenteeInd": "OWNER OCCUPIED",
        "propclass": "Apartment",
        "propsubtype": "SINGLE FAMILY",
        "proptype": "APARTMENT",
        "yearbuilt": 2006,
        "propLandUse": "APARTMENT"
      },
      "utilities": {
        "coolingtype": "TYPE UNKNOWN",
        "heatingtype": "WARM AIR",
        "wallType": "ALUMINUM"
      },
      "building": {
        "size": {
          "bldgsize": 2145,
          "grosssize": 0,
          "grosssizeadjusted": 0,
          "groundfloorsize": 0,
          "livingsize": 1738,
          "sizeInd": "LIVING SQFT ",
          "universalsize": 1738
        },
        "rooms": {
          "bathfixtures": 10,
          "baths1qtr": 0,
          "baths3qtr": 0,
          "bathscalc": 3,
          "bathsfull": 2,
          "bathshalf": 1,
          "bathstotal": 3,
          "beds": 3,
          "roomsTotal": 6
        },
        "interior": {
          "bsmtsize": 715,
          "bsmttype": "UNFINISHED",
          "fplccount": 0
        },
        "construction": {
          "condition": "GOOD",
          "wallType": "ALUMINUM"
        },
        "parking": {
          "prkgSize": 0,
          "prkgSpaces": "0"
        },
        "summary": {
          "archStyle": "TYPE UNKNOWN",
          "bldgsNum": 1,
          "bldgType": "SINGLE FAMILY",
          "levels": 2,
          "storyDesc": "SINGLE FAMILY",
          "unitsCount": "0",
          "yearbuilteffective": 0
        }
      },
      "vintage": {
        "lastModified": "2015-4-3",
        "pubDate": "2015-5-9"
      }
    }
  ]
}

访问我正在使用的数据

     $.ajax({
         type: "get",
         dataType: 'json',
         url: "https://search.onboard-apis.com/propertyapi/v1.0.0/property/detail?address1=7580%20Preservation%20Dr&address2=Sarasota%2C%20FL",
         beforeSend: function (request) {
             request.setRequestHeader("apikey", 'xxxxxxxxxxxxxxxxxxxx');
         },
         success: function (result) {
             $.each(result, function (index, object) {
                 $.each(object, function (i, o) {
                     console.log(i + " = " + o);
                 })
             })

         },
         error: function (xmlHttpRequest, textStatus, errorThrown) {
             output = '<div class="error">' + textStatus + ' ' + errorThrown + '</div>';
         }
     });

但是,我只得到以下输出。

version = 1.0.0
code = 0
msg = SuccessWithResult
total = 1
page = 1
pagesize = 10
0 = [object Object]

我想要做的是能够获取属性数据,以便从属性节点中提取特定的键/值。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

&#34;属性&#34;是一个数组本身,所以你需要在那里进行另一次迭代,然后再进行第二次迭代来扫描返回的对象。

如果您想要追踪特定的房产,可以采用更简单的方法。您可以使用     result.property [0] .area.munname;

直接访问munname。

答案 1 :(得分:0)

I actually got what I needed using

             console.log(result.property[0].address.oneLine)
             console.log(result.property[0].location.latitude)

thanks for all the help who contributed.