⑴ 如何通過js 獲取json數據
==========================================
js 中讀取JSON的方法探討
js讀取JSON的方法我接觸到的有兩種:
方法一:函數構造定義法返回
var strJSON = "{name:'json name'}";//得到的JSON
var obj = new Function("return" + strJSON)();//轉換後的JSON對象
alert(obj.name);//json name
方法二:js中著名的eval函數
var strJSON = "{name:'json name'}";//得到的JSON
var obj = eval( "(" + strJSON + ")" );//轉換後的JSON對象
alert(obj.name);//json name
第二種方法需要注意的是,對象表達式{'name':'json name'}必須用「()」擴住,否則
var strJSON = "{name:'json name'}";
var obj = eval(strJSON);
alert(obj.constructor);//String 構造函數
alert(obj.name);//undefine
必須把對象表達式擴起來eval執行才能生成一個匿名對象!
⑵ js 獲取到json 數據怎麼取裡面的數據
比如返回的json叫data
如果返回的是json字元串,那麼會自動轉換成相應的js對象
或者js對象組成的數組 [{"":"","":""}],
直接使用data[0]、data[1]·················就可以獲得
⑶ js 怎麼獲取json中的數據
var json = { contry:{ area:{ man:"12萬", women:"10萬" } } };
//方式一:使用eval解析
var obj = eval(json);
alert(obj.constructor);
alert(obj.contry.area.women);
//方式二:使用Funtion函數
var strJSON = "{name:'json name'}";//得到的JSON
var obj = new Function("return" + strJSON)();//轉換後的JSON對象
alert(obj.name);//json name
alert(obj.constructor);
//復雜一點的json數組數據的解析
var value1 = [{"c01":"1","c02":"2","c03":"3","c04":"4","c05":"5","c06":"6","c07":"7","c08":"8","c09":"9"}, {"c01":"2","c02":"4","c03":"5","c04":"2","c05":"8","c06":"11","c07":"21","c08":"1","c09":"12"}, {"c01":"5","c02":"1","c03":"4","c04":"11","c05":"9","c06":"8","c07":"1","c08":"8","c09":"2"}]; var obj1 = eval(value1);
alert(obj1[0].c01);
//復雜一點的json的另一種形式
var value2 = {"list":[ {"password":"1230","username":"coolcooldool"}, {"password":"thisis2","username":"okokok"}], "array":[{"password":"1230","username":"coolcooldool"},{"password":"thisis2","username":"okokok"}]};
var obj2 = eval(value2);
alert(obj2.list[0].password);