2016-03-04 100 views
0

我有一个嵌套的JSON对象,我在解析时遇到问题。该stuucture是如下使用jquery解析嵌套的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] 

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

任何帮助将不胜感激。

+1

您没有任何JSON? – adeneo

+0

在jQuery中,有一个函数getJSON() – Chuck

+1

而不是那个奇怪的嵌套循环,只是'console.log(result)'。 – Tomalak

回答

1

“property”本身就是一个数组,因此您需要通过那里进行另一次迭代,然后进行第二次迭代以扫描返回的对象。

如果您尝试追求特定属性,则有更简单的方法。您可以使用 result.property [0] .area.munname;

要直接访问munname。

+0

谢谢我已经试过,我只是不断在控制台中得到未定义。 – Rob

+0

成功:function(result){ console.log(result.property [0] .area.munname) }, – Rob

0

事实上,我什么,我需要使用

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

感谢所有谁贡献帮助。