用java怎么解析复杂的JSON
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用java怎么解析复杂的JSON相关的知识,希望对你有一定的参考价值。
success:0,
errorMsg:"错误消息",
data:
total:"总记录数",
rows:[
id:"任务ID",
workName:"任务名称",
assigneeName:"经办人姓名",
name:"流程步骤名称",
processInstanceInitiatorName:"发起人",
processInstanceStartTime:"发起时间",
createTime:"到达时间",
dueDate:"截止时间"
,
id:"ID",
workName:"名称",
assigneeName:"经办人",
name:"流程",
processInstanceInitiatorName:"发起人",
processInstanceStartTime:"发起",
createTime:"到达",
dueDate:"截止"
]
只是和客户间传数据,从客户那接收的是 JSON文件,然后解析 放到实体Bean里,方便调用
(能把代码写上感激不尽,还可以提高悬赏)
jakarta commons-lang 2.5
jakarta commons-beanutils 1.8.0
jakarta commons-collections 3.2.1
jakarta commons-logging 1.1.1
ezmorph 1.0.6
把上边的json定义为一个字符串 str,建议不要直接用还是自己动手写写。这样比较容易掌握方法。
JSONObject ob=JSONObject.fromObject(str);
Object success=ob.get("success");
Object errorMsg=ob.get("errorMsg");
System.out.println(success);
System.out.println(errorMsg);
JSONObject data=ob.getJSONObject("data");
Object total=data.get("total");
System.out.println(total);
JSONArray array=data.getJSONArray("rows");
JSONObject rows=null;
for(int i=0;i<array.size();i++)
rows=array.getJSONObject(i);
Object id=rows.get("id");
System.out.println(id);
Object workName=rows.get("workName");
System.out.println(workName);
Object assigneeName=rows.get("assigneeName");
System.out.println(assigneeName);
Object name=rows.get("name");
System.out.println(name);
Object processInstanceInitiatorName=rows.get("processInstanceInitiatorName");
System.out.println(processInstanceInitiatorName);
Object processInstanceStartTime=rows.get("processInstanceStartTime");
System.out.println(processInstanceStartTime);
Object createTime=rows.get("createTime");
System.out.println(createTime);
Object dueDate=rows.get("dueDate");
System.out.println(dueDate);
追问
那再怎么把解析出来的数据放到实体Bean里呢?谢谢
参考技术A 用JQUERY的 var data = $.parseJSON(str);var rows= data.rows
然后遍历出来
有的这样遍历不可以 你可以 在这里ALERT();一个值 看前面解析有没问题 如果没有问题 就是遍历后面的问题了
for (var i =0; i<rows.length; i++)
var id=rows[i].id;
var workName=rows[i].workName;
这样 不可以吗?追问
不想脚本,只想在后台处理
追答那后台处理 就是一楼说的方法了
JSONObject ob=JSONObject.fromObject(str);
这样转为JSONObject 对象 然后
单个只的 就直接用键取值
Object success=ob.get("success");
数组的 就取数组对象
JSONArray array=data.getJSONArray("rows");
出来 每个对象 你要用什么的 就GET什么出来就是了。。。。
至于你说的 放到实体bean 里
你取值出来 不是在实体bean生成了 GET set 方法吗?
那么 你NEW你的对象类出来 然后 SET转出来的值给它 哪里要 再GET出来 不就可以了吗?
一楼说的已经很好了 直接复制你都可以用了。。。。。
恩,是的,谢谢你啊
参考技术B 用json的包,JSONObject.fromObject(“xx”)java怎么使用gson解析json字符串
Gson是谷歌推出的解析json数据以及将对象转换成json数据的一个开源框架. 现在json因其易读性和高效率而被广泛的使用着.
相对于java以及其它json的解析框架,Gson非常的好用.
简单来讲就是根据json的数据结构定义出相应的javabean --->"new"出Gson的实例gson---->gson.fromJson(jsonString,JavaBean.class) 即可.
下面给出一个实例来说明.
步骤1:目标:将从webservice传回的json
"status": 0,
"result":
"location":
"lng": 103.98964143811,
"lat": 30.586643130352
,
"formatted_address": "四川省成都市双流县北一街154",
"business": "簇桥,金花桥",
"addressComponent":
"city": "成都市",
"district": "双流县",
"province": "四川省",
"street": "北一街",
"street_number": "154"
,
"cityCode": 75
先普及下json数据格式定义: json数据只有两种格式.
一种是对象: 一个大括号包裹的内容就是一个对象.里面是无数个逗号相间隔的键值对
"firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa"一种是数组:一个方括号包裹的内容就是一个数组,里面是无数个逗号相间隔的json对象
如:
"people": [
"firstName": "Brett",
"lastName": "McLaughlin",
"email": "aaaa"
,
"firstName": "Jason",
"lastName": "Hunter",
"email": "bbbb"
,
"firstName": "Elliotte",
"lastName": "Harold",
"email": "cccc"
]
步骤2 定义json数据格式对应的javaBean
private Integerstatus;
private ResultDetailresult;
public Result()
public Result(Integer status, ResultDetail result)
super();
this.status = status;
this.result = result;
public ResultDetail getResult()
return this.result;
public Integer getStatus()
return this.status;
public void setResult( ResultDetail result )
this.result = result;
public void setStatus( Integer status )
this.status = status;
@Override
public String toString()
return "Result [status=" + this.status + ", result=" + this.result
+ "]";
public class ResultDetail
Locationlocation;
Stringformatted_address;
AddressComponentaddressComponent;
Stringbusiness;
StringcityCode;
public ResultDetail()
super();
// TODO Auto-generated constructor stub
public ResultDetail(Location location, String formatted_address,
AddressComponent addressComponent, String business, String cityCode)
super();
this.location = location;
this.formatted_address = formatted_address;
this.addressComponent = addressComponent;
this.business = business;
this.cityCode = cityCode;
public AddressComponent getAddressComponent()
return this.addressComponent;
public String getBusiness()
return this.business;
public String getCityCode()
return this.cityCode;
public String getFormatted_address()
return this.formatted_address;
public Location getLocation()
return this.location;
public void setAddressComponent( AddressComponent addressComponent )
this.addressComponent = addressComponent;
public void setBusiness( String business )
this.business = business;
public void setCityCode( String cityCode )
this.cityCode = cityCode;
public void setFormatted_address( String formatted_address )
this.formatted_address = formatted_address;
public void setLocation( Location location )
this.location = location;
public class Location
Stringlng;
Stringlat;
public Location()
public Location(String lng, String lat)
this.lng = lng;
this.lat = lat;
public String getLat()
return this.lat;
public String getLng()
return this.lng;
public void setLat( String lat )
this.lat = lat;
public void setLng( String lng )
this.lng = lng;
@Override
public String toString()
return "Location [lng=" + this.lng + ", lat=" + this.lat + "]";
public class AddressComponent
Stringcity;
Stringdistrict;
Stringprovince;
Stringstreet;
Stringstreet_number;
public AddressComponent()
super();
// TODO Auto-generated constructor stub
public AddressComponent(String city, String district, String province,
String street, String street_number)
super();
this.city = city;
this.district = district;
this.province = province;
this.street = street;
this.street_number = street_number;
public String getCity()
return this.city;
public String getDistrict()
return this.district;
public String getProvince()
return this.province;
public String getStreet()
return this.street;
public String getStreet_number()
return this.street_number;
public void setCity( String city )
this.city = city;
public void setDistrict( String district )
this.district = district;
public void setProvince( String province )
this.province = province;
public void setStreet( String street )
this.street = street;
public void setStreet_number( String street_number )
this.street_number = street_number;
@Override
public String toString()
return "AddressComponent [city=" + this.city + ", district="
+ this.district + ", province=" + this.province + ", street="
+ this.street + ", street_number=" + this.street_number + "]";
测试:
jsonString ( 目标json数据,已经在最上面写好的)
Gson gson = new Gson();
Result fromJson = gson.fromJson( jsonString.toString() ,Result.class );
System.out.println( "******************************************" );
System.out.println( fromJson );
结果:
jsonString:"status":0,"result":"location":"lng":103.98964143811,"lat":30.586643130352,"formatted_address":"四川省成都市双流县北一街154","business":"簇桥,金花桥","addressComponent":"city":"成都市","district":"双流县","province":"四川省","street":"北一街","street_number":"154","cityCode":75*******************************************
Result [status=0, result=ResultDetail [location=Location [lng=103.98964143811, lat=30.586643130352], formatted_address=四川省成都市双流县北一街154, addressComponent=AddressComponent [city=成都市, district=双流县, province=四川省, street=北一街, street_number=154], business=簇桥,金花桥, cityCode=75]]
可见,jsonString已经成功的被转换成了对应的javaBean
步骤3 : 总结.说明
Gson可以很轻松的实现javaBean和jsonString之间的互转.只需要明白json如何定义.剩下的就非常简单了.
参考技术A 步骤1:目标:将从webservice传回的json1
"status": 0,
"result":
"location":
"lng": 103.98964143811,
"lat": 30.586643130352
,
"formatted_address": "四川省成都市双流县北一街154",
"business": "簇桥,金花桥",
"addressComponent":
"city": "成都市",
"district": "双流县",
"province": "四川省",
"street": "北一街",
"street_number": "154"
,
"cityCode": 75
2
先普及下json数据格式定义: json数据只有两种格式.
一种是对象: 一个大括号包裹的内容就是一个对象.里面是无数个逗号相间隔的键值对
"firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa"
一种是数组:一个方括号包裹的内容就是一个数组,里面是无数个逗号相间隔的json对象
如:
"people": [
"firstName": "Brett",
"lastName": "McLaughlin",
"email": "aaaa"
,
"firstName": "Jason",
"lastName": "Hunter",
"email": "bbbb"
,
"firstName": "Elliotte",
"lastName": "Harold",
"email": "cccc"
]
END
步骤2 定义json数据格式对应的javaBean
public class Result
private Integer status;
private ResultDetail result;
public Result()
public Result(Integer status, ResultDetail result)
super();
this.status = status;
this.result = result;
public ResultDetail getResult()
return this.result;
public Integer getStatus()
return this.status;
public void setResult( ResultDetail result )
this.result = result;
public void setStatus( Integer status )
this.status = status;
@Override
public String toString()
return "Result [status=" + this.status + ", result=" + this.result
+ "]";
参考技术B 去官网下载jar包,调用方法。
以上是关于用java怎么解析复杂的JSON的主要内容,如果未能解决你的问题,请参考以下文章