PDA

View Full Version : extracting data from json



proofmoore
20 Sep 2010, 11:41 PM
hi guys,
i can alert the json which prints out:

" 'myroot':[{'fid': 'koy_bina.133','name':'Alt?nus','tip':'Kerpic','detay':'','id':'133'}] "


function getResukt(result){
var jsonData = (result.responseText);
alert(jsonData)
};

can i get the datas seperately? for example; how can i get the name here?

thanks.

StuartAshworth
21 Sep 2010, 12:14 AM
You need to decode the responseText into a JSON object as it at the point you are alerting it is only text.




var jsonResponse = Ext.util.JSON.decode(result.responseText);

alert(jsonResponse.myroot[0].name);

proofmoore
21 Sep 2010, 12:32 AM
i tiried it and i 'm getting this error from firebug:


missing ) in parenthetical
[Break on this error] var swfobject=function(){var E="undefi...);Ext.grid.GroupingView.GROUP_ID=1000;
ext-all.js (line 11)

StuartAshworth
21 Sep 2010, 12:40 AM
I dont think your JSON string is valid. Use http://www.jsonlint.com/ to validate it.

laurentParis
21 Sep 2010, 12:43 AM
it's normal that


" 'myroot':[{'fid': 'koy_bina.133','name':'Alt?nus','tip':'Kerpic','detay':'','id':'133'}] "

=> is on error

correct json is


" {'myroot':[{'fid': 'koy_bina.133','name':'Alt?nus','tip':'Kerpic','detay':'','id':'133'}] } "

proofmoore
21 Sep 2010, 2:46 AM
now my json string is :

"{'baskil_koy_bina':[{'fid': 'baskil_koy_bina.50','koy_isim':'Alt?nu?a??','tip':'Okul','detay':'','id':'50'}]}"

code:

function setHTML(result){
var jsonResponse = Ext.util.JSON.decode(result.responseText);
alert(jsonResponse.baskil_koy_bina[0].koy_isim)
}

firebug:

jsonResponse.baskil_koy_bina is undefined
[Break on this error] alert(jsonResponse.baskil_koy_bina[0].koy_isim)

sorry for having your time

StuartAshworth
21 Sep 2010, 3:07 AM
Are you sure the "result" parameter is set to what you're expecting? If you do the decode and alert in FireBug it works fine.



//paste into firebug

var jsonResponse = Ext.util.JSON.decode("{'baskil_koy_bina':[{'fid': 'baskil_koy_bina.50','koy_isim':'Alt?nu?a??','tip':'Okul','detay':'','id':'50'}]}");

alert(jsonResponse.baskil_koy_bina[0].koy_isim)

proofmoore
21 Sep 2010, 3:23 AM
i am very confused here : (

var json = result.responseText;
alert(json)

this alerts the same string which is in your code but still says "jsonResponse.baskil_koy_bina is undefined".
how can they behave different i dont get it

proofmoore
21 Sep 2010, 3:44 AM
i decoded it two times and worked finally.


var json = result.responseText;
var jsonResponse = Ext.util.JSON.decode(json);
var jsonResponse2 = Ext.util.JSON.decode(jsonResponse).baskil_koy_bina[0].koy_isim;
alert(jsonResponse2)

but i still dont get it : )