⑴ 如何获取网页中的json数据
1、首先打开你编写网页程序的软件。
⑵ 如何获取数据并转成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>
⑶ 如何从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数据
使用jq的each方法
$.each(数组,function(key,value){
//循环操作
})
也可以使用js写一个each方法模拟jq的each
Array.prototype.each=function(fn){returnthis.length?[fn(this.slice(0,1))].concat(this.slice(1).each(fn)):[];};
上面的部分直接贴在js里,使用方法如下
数组.each(function(value){
//循环操作
});
⑸ json取数据怎么取
根据返回的字符串可以看出是多个json拼接成的js数组的形式。
如果返回的只是字符串须现将字符串转成js对象。
使用Jquery的$.each()方法循环js数组取出每一个json对象的数据。
str = '[{"key":"value","keys":[{"key1":"value1","key2":"value2"},{"key1":"value3","key2":"value4"}],"obj":{"id":1,"msg":"success"}}]';
str_json = eval("(" + str + ")");//将字符串转换成js对象
$.each(str_json,fucntion(a,b){
alert(a);//弹出数组的key
alert(b.id);//弹出要取出的数据
});
⑹ 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);
⑺ 如何获取JSON返回的数据
JSONArray json = JSONArray.fromObject(models); //list集合数据转json
response.getWriter().println(json);//传到前台
//ajax
success: function(data){
var models = eval("("+data+")");
var rows="";
for(var i in models){
rows =rows + "<tr>"+
"<td>"+models[i].statDate+"</td>"+
"<td>"+models[i].endDate+"</td>"+
⑻ 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
⑼ java 怎么取json 字段的数据
JSONObjectj=newJSONObject();
j.put("id","22");
j.put("name","haha");
j.put("sex","xixi");
System.out.println(j.get("id"));