㈠ 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>