㈠ js怎樣讀取json裡面的數據
varjson={contry:{area:{man:"12萬",women:"10萬"}}};
//方式一:使用eval解析
varobj=eval(json);
alert(obj.constructor);
alert(obj.contry.area.women);
//方式二:使用Funtion函數
varstrJSON="{name:'jsonname'}";//得到的JSON
varobj=newFunction("return"+strJSON)();//轉換後的JSON對象
alert(obj.name);//jsonname
alert(obj.constructor);
//復雜一點的json數組數據的解析
varvalue1=[{"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"}];varobj1=eval(value1);
alert(obj1[0].c01);
//復雜一點的json的另一種形式
varvalue2={"list":[{"password":"1230","username":"coolcooldool"},{"password":"thisis2","username":"okokok"}],"array":[{"password":"1230","username":"coolcooldool"},{"password":"thisis2","username":"okokok"}]};
varobj2=eval(value2);
alert(obj2.list[0].password);
㈡ 如何獲取get中的json數據
要回答這問題,首先要理解ajax是非同步訪問的,所謂非同步,類似於多線程,js解釋器執行到ajax的時候,並不會等待返回結果以及回調函數都執行完了過後才執行下一條語句(在這里就是alert語句)
㈢ jq 怎麼獲取json 數據的值
如果是後台數據就用ajax獲取,這樣獲取後的數據一般為json數據。如是前端數據轉為json數據就用JSON.parse方法。如:
vars='[1,2,3]';vararr=JSON.parse(s);//arr=[1,2,3]
㈣ 怎麼獲取json中每一列的數據
json啊a值字元串數組應該 var json={ a:["aaaaa","bbbbb","ccccc"], b:["11111","22222","33333"] }; 單引號掉才數組呢 用json.a[1]訪問bbbbb
希望回答能給你帶來幫助,如果還又不明白的地方請追問
你的採納是我前進的動力答題不易互相理解
㈤ 如何從json中取數據
只能用循環,代碼如下:
[html] view plain
var head_id = "";
var head_pid = "";
for (var i = 0; i < data.head.length; i++) {
head_id += data.head[i].id + " "; //循環輸出json數據
head_pid += data.head[i].pid + " ";
}
$("#city").append("city:" + head_id);
$("#city").append("province:" + head_pid);
這樣,將會在data之後,依次輸出json中的數據。
㈥ 怎麼獲取json數據的key值
var obj = {"id": 1, "name":"張三"};
for(var key in obj)
{
alert("Key是:" + key);
alert("對應的值是:" + obj[key]);
}
㈦ 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);
㈧ java怎麼讀取json格式的數據
java可以使用JSONObject和JSONArray來操作json對象和json數組,具體用法如下
1:java對象與json串轉換:
java對象—json串:
JSONObject JSONStr = JSONObject.fromObject(object);
String str = JSONStr.toString();
json串—java對象:
JSONObject jsonObject = JSONObject.fromObject( jsonString );
Object pojo = JSONObject.toBean(jsonObject,pojoCalss);
2:java數組對象與json串轉換:
java數組—json串:
JSONArray arrayStr = JSONArray.fromObject(List<?>);
String str = arrayStr.toString();
json串—java數組:
JSONArray array = JSONArray.fromObject(str);
List<?> list = JSONArray.toList(array, ?.class);
㈨ java怎麼得到json中的數據
如果不是Android開發環境的話,首先需要引入處理JSON數據的包:json-lib-2.2.3-jdk15.jar
Java樣常式序如下:
importnet.sf.json.JSONArray;
importnet.sf.json.JSONObject;
publicclassDoJSON{
publicstaticvoidmain(String[]args){
JSONArrayemployees=newJSONArray(); //JSON數組
JSONObjectemployee=newJSONObject(); //JSON對象
employee.put("firstName","Bill"); //按「鍵-值」對形式存儲數據到JSON對象中
employee.put("lastName","Gates");
employees.add(employee); //將JSON對象加入到JSON數組中
employee.put("firstName","George");
employee.put("lastName","Bush");
employees.add(employee);
employee.put("firstName","Thomas");
employee.put("lastName","Carter");
employees.add(employee);
System.out.println(employees.toString());
for(inti=0;i<employees.size();i++){
JSONObjectemp=employees.getJSONObject(i);
System.out.println(emp.toString());
System.out.println("FirstName: "+emp.get("firstName"));
System.out.println("LastName: "+emp.get("lastName"));
}
}
}
運行效果:
[{"firstName":"Bill","lastName":"Gates"},{"firstName":"George","lastName":"Bush"},{"firstName":"Thomas","lastName":"Carter"}]
{"firstName":"Bill","lastName":"Gates"}
FirstName : Bill
LastName : Gates
{"firstName":"George","lastName":"Bush"}
FirstName : George
LastName : Bush
{"firstName":"Thomas","lastName":"Carter"}
FirstName : Thomas
LastName : Carter
㈩ 如何獲取數據並轉成json數據
感覺還是有點復雜,可用。可加雙引號。
<!DOCTYPEhtml>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<scriptsrc="jquery-2.0.2.min.js"></script>
<title>無標題文檔</title>
<scriptlanguage="javascript"type="text/javascript">
window.onload=function(){
//tabToJSON("tab");
tabToJSONForJquery("tab");
};
functiontabToJSON(id){
vartrs=document.getElementById(id).getElementsByTagName("tr");//獲得tr數組
vartitles=trs[0].getElementsByTagName("td");//獲得表頭td數組
varjson="";
for(vari=1;i<trs.length;i++){
vartds=trs[i].getElementsByTagName("td");
json+="{";
//拼裝json
for(varj=0;j<tds.length;j++)
json+=titles[j].innerHTML+":"+tds[j].innerHTML+",";
json=json.substring(0,json.length-1)+"},";
}
json="["+json.substring(0,json.length-1)+"]";
document.getElementById("test").innerHTML=json;
}
functiontabToJSONForJquery(id){
vartitles=$("#"+id).find("tr:firsttd");//獲得表頭td數組
//遍歷非表頭的,tr、td...拼裝json
varjson="["+$("#"+id).find("tr:not(:first)").map(function(i,e){
return"{"+$(e).children("td").map(function(j,el){
return$(titles[j]).html()+":"+$(el).html();
}).get().join(",")+"}";
}).get().join(",")+"]";
$("#test").html(json);
}
</script>
</head>
<body>
<tableid="tab">
<tr>
<td>test1</td>
<td>test2</td>
<td>test3</td>
</tr>
<tr>
<td>val1</td>
<td>val2</td>
<td>val3</td>
</tr>
<tr>
<td>val11</td>
<td>val22</td>
<td>val33</td>
</tr>
</table>
<divid="test"></div>
</body>
</html>