2015-08-27 101 views
0

我剛與Json合作,我想從Json數組獲取特定Id數據。
這是我使用的api網址。如何通過URL過濾獲得特定的Json數據?

http://example/plapi/areas?jsoncallback=? 

此URL返回JSON數組一樣

([{"area_id":"25","ca_id":"0","area_name":"Durgapuram","alias_area":"","area_sp‌​l":"No","area_lat":"16.52069","area_lng":"80.64067","area_desc":"","area_image":"‌​","map_image":"","area_status":"1","city_id":"27","position":"0","m_keyword":""},‌​{"area_id":"26","ca_id":"0","area_name":"Arundalpet","alias_area":"","area_spl":"‌​No","area_lat":"16.30520","area_lng":"80.44169","area_desc":"","area_image":"","m‌​ap_image":"","area_status":"1","city_id":"31","position":"0","m_keyword":""})] 

在我想要得到的數據只是city_id = 31。

要獲得其ID數據爲31,我在做這樣的

var data = { 
"id": "31", 
}; 
$.getJSON("http://example/plapi/areas?jsoncallback=?",JSON.stringify(data) ,function(result){console.log(result); 
}); 

但它返回什麼。

我想只獲得31個id關聯的數據而不是其他人。請幫我怎麼做?

+0

是不是creal你所需要的。你需要解析json並提取一些數據,或者你需要從後端獲取一些特定的數據? – IgnazioC

+0

我不認爲'jsoncallback'被用來定義你的查詢的'id'。 – MinusFour

+0

上面的url返回所有區域im,具有Json數組格式,但我需要的數據只有id爲31 – user3668438

回答

0

找JS代碼從您的JSON只有City_ID獲取: -

var jsonData = [ 
 
    { 
 
     
 
     "area_id": "25", 
 
     "ca_id": "0", 
 
     "area_name": "Durgapuram", 
 
     "alias_area": "", 
 
     "area_spl": "No", 
 
     "area_lat": "16.52069", 
 
     "area_lng": "80.64067", 
 
     "area_desc": "", 
 
     "area_image": "1", 
 
     "map_image": "", 
 
     "area_status": "1", 
 
     "city_id": "27", 
 
     "position": "0", 
 
     "m_keyword": "" 
 
    }, 
 
    { 
 
     
 
     "area_id": "26", 
 
     "ca_id": "0", 
 
     "area_name": "Arundalpet", 
 
     "alias_area": "", 
 
     "area_spl": "No", 
 
     "area_lat": "16.30520", 
 
     "area_lng": "80.44169", 
 
     "area_desc": "", 
 
     "area_image": "", 
 
     "map_image": "", 
 
     "area_status": "1", 
 
     "city_id": "31", 
 
     "position": "0", 
 
     "m_keyword": "" 
 
    } 
 
]; 
 

 
for(var obj in jsonData){ 
 
    if(jsonData.hasOwnProperty(obj)){ 
 
    for(var prop in jsonData[obj]){ 
 
     if(jsonData[obj].hasOwnProperty(prop)){ 
 
      if(prop=="city_id"){ 
 
       prop; 
 
       alert(prop); 
 
       break; 
 
      } 
 

 
     } 
 
    } 
 
} 
 
}

樂意幫助

+0

我不想限制使用if條件的值,想要通過在'$ .getJson('url',{city_id = 31})'中傳遞值來過濾url本身。 – user3668438