在线解析json数据
成考报名 发布时间:10-13 阅读:
JS解析JSON数据及取值的用法
在线解析json数据 第一篇
JS解析JSON数据及取值的一些用法
<%@ page contentType="text/html;charset=GBK" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" /> <title>JS解析JSON数据</title>
</head>
<body style="margin:50px">
<center><h1><b>JS解析JSON数据测试界面</b></h1></center>
<script language="javascript">
//将JSON字符串转换为JSON对象的一些方法
//将JSON字符串转换为JSON对象的一些用法
(function (){
var str = {"name":"张三","age":20};//JSON对象
alert(str.name);alert(str.age);//直接取JSON对象的值
//将JSON字符串转换为JSON对象
var str = "{\"name\":\"张三\",\"age\":\"21\"}";//JSON字符串 转义不用系统/和dos一致用\
var obj = eval('(' + str + ')');//使用eval函数将JSON字符串转换为JSON对象 alert(obj.name);alert(obj.age);
var str = '{"name":"张三","age":22}';//JSON字符串
var obj = (new Function("return " + str))(); //使用new Function()形式将JSON字符串转换为JSON对象
alert(obj.name);alert(obj.age);
//将JSON对象转换为JSON字符串
//var straa = {"name":"李四","age":"20"};//JSON对象
//var newJSONtext = straa.toJSONString();//貌似没有原生方法
//alert("JSON对象转换为JSON字符串: "+newJSONtext);
})();//(function (){})();自动执行匿名函数
</script>【在线解析json数据】
<script>
//取值的一些用法
//取值的一些用法
function showJSON() {
//效果一
var user =
{
"username":"andy",
"age":20,
"info": { "tel": "123456", "cellphone": "98765"},
"address":
[
{"city":"beijing","postcode":"222333"},
{"city":"newyork","postcode":"555666"}
]
}
alert(user.username);
alert(user.age);
alert(user.info.cellphone);
alert(user.address[0].city);
alert(user.address[0].postcode);
user.username = "Tom";
alert(user.username);
//效果二
var people =
{ "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance.com" },
![【在线解析json数据】](http://www.apkbus.com/data/attachment/forum/201404/14/174233ly6kcncjykjkcbhu.jpg)
{ "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets.com" }, { "firstName": "Elliotte", "lastName":"Harold", "email": "elharo@macfaq.com" } ],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" } ],
![【在线解析json数据】](http://www.0898pk.com/upload/img/pNPdgNQcmYOw3hfPpivZI472LpEO7qxSrDp-GXcGUkYaVdXuOjote1abOJvJ9HX8L6WVQQGTz5RmUAxwxVI4UYp84H-U4alnTd1WHEg25nDm4Q.jpg)
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" } ]
}
alert(people.programmers[0].firstName+" "+people.programmers[0].email); alert(people.programmers[2].firstName+" "+people.programmers[2].email);
alert(people.authors[0].firstName+" "+people.authors[0].genre);
alert(people.musicians[1].firstName+" "+people.musicians[1].instrument);
}
showJSON();
</script>
</body>
</html>
<script type="text/javascript">
function mm()
{
var str = "";
for (mm in window.event.srcElement)
str += mm+"="+ window.event.srcElement[mm] +"\n"; //得到 srcElement 的所有属性
alert(str);
}
</script>
解析JSON数据
在线解析json数据 第二篇
生活指南原始JSON数据:
{
"results": [
{
"location": {
"id": "WHX38U36B3BU",
"name": "大理",
"country": "CN",
"path": "大理,大理,云南,中国",
"timezone": "Asia/Shanghai",
"timezone_offset": " 08:00"
},
"suggestion": {
"car_washing": {
"brief": "不宜",
"details": "不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。"
},
"dressing": {
"brief": "舒适",
"details": "建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。"
},
"flu": {
"brief": "少发",
"details": "各项气象条件适宜,无明显降温过程,发生感冒机率较低。" },
"sport": {
"brief": "较不宜",
"details": "有降水,推荐您在室内进行健身休闲运动;若坚持户外运动,须注意保暖并携带雨具。"
},
"travel": {
"brief": "适宜",
"details": "有降水,温度适宜,在细雨中游玩别有一番情调,可不要错过机会呦!但记得出门要携带雨具。"
},
"uv": {
"brief": "弱",
"details": "紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA 的防晒护肤品。"
}
},
"last_update": "2016-06-10T16:40:10 08:00"
}
]
}
实时天气情况原始JSON数据:
{
"results": [
{
"location": {
"id": "WHX38U36B3BU",
"name": "大理",
"country": "CN",
"path": "大理,大理,云南,中国",
"timezone": "Asia/Shanghai",
"timezone_offset": " 08:00"
},
"now": {
"text": "阵雨",
"code": "10",
"temperature": "19"
},
"last_update": "2016-06-10T16:25:00 08:00"
}
]
}
未来几天天气预报原始JSON数据:【在线解析json数据】
{
"results": [
{
"location": {
"id": "WHX38U36B3BU",
"name": "大理",
"country": "CN",
"path": "大理,大理,云南,中国",
"timezone": "Asia/Shanghai",
"timezone_offset": " 08:00"
},
"daily": [
{
"date": "2016-06-10",
"text_day": "阵雨",
"code_day": "10",
"text_night": "阵雨",
"code_night": "10",
"high": "24",
"low": "16",
"precip": "",
"wind_direction": "无持续风向",
"wind_direction_degree": "",
"wind_speed": "10",
"wind_scale": "2"
},
{
"date": "2016-06-11",
"text_day": "阵雨",
"code_day": "10",
"text_night": "小雨",
"code_night": "13",
"high": "22",
"low": "17",
"precip": "80",
"wind_direction": "无持续风向",【在线解析json数据】
"wind_direction_degree": "",
"wind_speed": "10",
"wind_scale": "2"
},
{
"date": "2016-06-12",
"text_day": "中雨",
"code_day": "14",
"text_night": "小雨",
"code_night": "13",
"high": "24",
"low": "17",
"precip": "90",
"wind_direction": "无持续风向",
"wind_direction_degree": "",
"wind_speed": "10",
"wind_scale": "2"
}
],
"last_update": "2016-06-10T11:00:00 08:00"
}
]
}
解析JSON:
package com.lastexam.weatherforecast;
import java.util.ArrayList;
import java.util.Scanner;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class ParseJSON
{
/*
public static void main(String []args)
{
Scanner in=new Scanner(System.in);
System.out.println("请输入数据:");
String string=in.next();
System.out.println(ParseResult(string));
Location location=new Location();
location=ParseLocation(ParseResult(string)); System.out.println(location.toString());
//实时天气情况解析完毕
RealNow now=new RealNow();
now=ParseNow(ParseResult(string));
System.out.println(now.toString());
//天气预报解析完毕
WeatherDaily []daily=ParseDaily(ParseResult(string)); for(int i=0;i<daily.length;i )
{
System.out.println(daily[i].toString()); }
//suggestion解析完毕
SuggestionCarWashing carWashing=new
SuggestionCarWashing();
carWashing=ParseCarWashing(ParseResult(string)); System.out.println(carWashing.toString());
SuggestionDressing dressing=new SuggestionDressing(); dressing=ParseDressing(ParseResult(string)); System.out.println(dressing.toString());
SuggestionFlu flu=new SuggestionFlu();
flu=ParseFlu(ParseResult(string));
System.out.println(flu.toString());
SuggestionSport sport=new SuggestionSport(); sport=ParseSport(ParseResult(string));
System.out.println(sport.toString());
SuggestionTravel travel=new SuggestionTravel(); travel=ParseTravel(ParseResult(string));
System.out.println(travel.toString());
SuggestionUv uv=new SuggestionUv();
uv=ParseUv(ParseResult(string));
System.out.println(uv.toString());
System.out.println(ParseUpdateTime(ParseResult(string))); }
*/
//将原始的JSON数据解析之后返回(主要是提取result中的内容) public String ParseResult(String originalJSON)
{
JSONObject
resultsJSONObject=JSONObject.fromObject(originalJSON);
JSONArray
resultsJSONArray=resultsJSONObject.getJSONArray("results");
return resultsJSONArray.getJSONObject(0).toString(); }
//提取location中的内容,并且将提取出来的内容放到Location对象中并返回
public Location ParseLocation(String resultJSON)
{
Location location=new Location();
JSONObject
locationJSONObjectOriginal=JSONObject.fromObject(resultJSON); String
locationString=locationJSONObjectOriginal.getString("location");
后台转换JSON数据类型,前台解析JSON数据等等例子总结
在线解析json数据 第三篇
后台转换JSON数据类型,前台解析JSON数据等等例子总结 JSON对象:
JSONObject obj = new JSONObject();
return obj.toJSONString();
前台解析: Success:function(data){
var result = data.result;
var message = data.message; }
json对象中有json对象的写法:
Objstr为一个JSONObject obj的String转换
private String getsuccess(String objstr,int number){
JSONObject obj = new JSONObject();
return obj.toJSONString();
}
前台解析:
Picjson为success返回的data
var result = picjson.result;
if (result==1) {
var objdata = picjson.obj;
var data = eval('(' + objdata + ')'); var num = picjson.number;
picurl = null;
showpiclist(data, num);
} else{
alert(picjson.message);
picurl = null;
}
list转成json对象
需要的包:
1.commons-lang.jar
2.commons-beanutils.jar
3.commons-collections.jar
4.commons-logging.jar
5.ezmorph.jar
6.json-lib-2.2.2-jdk15.jar ,
由于在struts2中没有自带的.ezmorph.jar ,一开始List对象列表转换成json数据格式的时候并不对还报错 List<ImageBean>
bean=newmainservice.getImageBeanbyNewid(newid); JSONArray
json=JSONArray.fromObject((Object)bean); System.out.println(json.toString());
response.getWriter().print(json.toString()); }catch(Exception e){e.printStackTrace();} 前台解析: $.ajax({
method:"post",
url:"/bs.cxhd/pub/newpiclist.action", data:{"newid":newid},
success:function(data){
var piclist=eval(data);
alert(piclist.length);
alert(piclist[0]["url"]);
},
error:function(data){} 因为时间有限,我就不贴那么多代码了,剩下的自己看看就懂额。毕设有需要的联系QQ2978077563
后期即将发布几个中小型的web项目,需要源码的请加QQ