2011-10-29 12 views
1

常に同じ方法でフォーマットされているわけではないJSON応答の特定の属性を識別できる必要があります。例の抜粋です:あなたが名前を見ることができるように、表示名と値は、属性ごとに繰り返されjQuery - jsonの特定の属性を見つける

{ 
    "Attributes" : [ 
     { 
     "Value" : "4 bedrooms", 
     "DisplayName" : "Bedrooms", 
     "Name" : "bedrooms" 
     }, 
     { 
     "Value" : "2 bathrooms", 
     "DisplayName" : "Bathrooms", 
     "Name" : "bathrooms" 
     }, 
     { 
     "Value" : "House", 
     "DisplayName" : "Property type", 
     "Name" : "property_type" 
     }, 
     { 
     "Value" : "$780,000", 
     "DisplayName" : "Rateable value", 
     "Name" : "rateable_value" 
     }, 
     { 
     "Value" : "Price by negotiation", 
     "DisplayName" : "Price", 
     "Name" : "price" 
     }, 
     { 
     "Value" : "13 Wellswood Way\r\nLower Shotover\r\nQueenstown-Lakes\r\nOtago", 
     "DisplayName" : "Location", 
     "Name" : "location" 
     }, 
     { 
     "Value" : "Queenstown-Lakes", 
     "DisplayName" : "District", 
     "Name" : "district" 
     }, 
     { 
     "Value" : "Lower Shotover", 
     "DisplayName" : "Suburb", 
     "Name" : "suburb" 
     }, 
     { 
     "Value" : "Otago", 
     "DisplayName" : "Region", 
     "Name" : "region" 
     }, 
     { 
     "Value" : "254m²", 
     "DisplayName" : "Floor area", 
     "Name" : "floor_area" 
     }, 
     { 
     "Value" : "1690m²", 
     "DisplayName" : "Land area", 
     "Name" : "land_area" 
     }, 
     { 
     "Value" : "CBM959", 
     "DisplayName" : "Property ID#", 
     "Name" : "property_id" 
     }, 
     { 
     "Value" : "playground,tennis,hiking,biking,historic bridge walk,buses,5 mins to shops.", 
     "DisplayName" : "In the area", 
     "Name" : "in_the_area" 
     }, 
     { 
     "Value" : "Large double garage.Plenty off-street parking.Extra for boat+ etc.", 
     "DisplayName" : "Parking", 
     "Name" : "parking" 
     } 
    ] 
} 

。どのようにすればよいかわからないのは、これらの属性を調べて特定の属性を見つけることです。たとえば、私はBathroomsの価値をどのように得ることができますか?あなたの助けのための

おかげ アダム

答えて

0
function getAttributesByName(arr,name){ 
    for(var i=0,l=arr.length;i<l;i++) 
     if(arr[i].Name === name) 
      return arr[i]; 
    return null;    
} 

よりjqueryのアプローチ:

function getAttributesByName(arr,name){ 
    var result = null; 
    $.each(arr,function(){ 
     if(this.Name === name) 
      result = this; 
    }); 
    return result; 
} 
0

ここにあります:

var a = data.Attributes; 
for(var i in a) { 
    if(a.hasOwnProperty(i) && a[i].Name == 'bathrooms') 
     return a[i].Value; 
} 
関連する問題