JavaJSONObject与JSONArray对象案例详解
JSONObject与JSONArray
最近在学习过程中⽤到了稍微复杂点的json数据需要将json数据解析出来,这⾥就截取⼀部分作为例⼦
1.JSONObject介绍
JSONObject-lib包是⼀个beans,collections,maps,java arrays和xml和JSON互相转换的包。
2.下载jar包
*或者在Maven的l⽂件中直接配置如下:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
json数据:
{
"cartypes":[
{"id":1,"imgUrl":"img/l.jpg","bigimg": "img/d.jpg","title":"别克威朗","marketprice":"15.29","periods":"12",
"endrepayone":"96800","endrepaytwo":"96800","endrepaythree":"93000",
"endmonthone":"3408","endmonthtwo":"3408","endmonththree":"3278",
"repayfirst":"15290","repaytwo":"22935", "repaythree":"30580",
"monthrepayone":"3578","monthrepaytwo":"2878","monthrepaythree":"2478",
"cardetails": [{
"imageId00": "img/first-bkwl.jpg",
"imageId01": "img/bkwl01.jpg",
"imageId02": "img/bkwl02.jpg",
"imageId03": "img/bkwl03.jpg",
"imageId04": "img/bkwl04.jpg",
"carname": "别克",
"carmatter": "威朗",
"carvolume":"1.5L",
"sitnum":"5",
"cargearbox":"6挡⼿⾃⼀体",
"caremission":"国V",
"carldone":"⼀体式座舱",
"carldtwo":"绒⾯内饰",
"carldthree":"全景天窗",
"carldfour":"展翼型HID⼤灯"
}]
},
{"id":2,"imgUrl":"img/l.jpg","bigimg": "img/d.jpg","title":"英菲尼迪","marketprice":"18.98","periods":"12",
"endrepayone":"126800","endrepaytwo":"126800","endrepaythree":"126800",
"endmonthone":"4458","endmonthtwo":"4458","endmonththree":"4458",
"repayfirst":"18980","repaytwo":"28470", "repaythree":"37960",
"monthrepayone":"2738","monthrepaytwo":"1878","monthrepaythree":"998",
"cardetails": [{ "imageId00": "img/first.jpg",
"imageId01": "img/yfnd01.jpg",
"imageId02": "img/yfnd02.jpg",
"imageId03": "img/yfnd03.jpg",
"imageId04": "img/yfnd04.jpg",
"carname": "英菲尼迪",
"carmatter": "ESQ",
"carvolume":"1.6L",
"sitnum":"5",
"cargearbox":"CVT⽆级变速",
"caremission":"国V",
"carldone":"定制轮毂",
"carldtwo":"多功能⽅向盘",
"carldthree":"LED尾灯",
"carldfour":"真⽪座椅"
}] } ]
}
当接受到的是上⾯的json数据时,要获取到⾥⾯的键对应的值应该怎样做呢,⽐如要获取title的值,获取cardetails中的imageId02的值等。
⾯对这样数组与对象相互嵌套的情况需要⼀步步将数据拆分,主要思想还是根据键取值,对于数组类型还是需要先根据”下标”取出元素。这⾥还需要⽤到JSONObject与JSONArray。
将上⾯的json数据简化就是:(这⾥保留个id便于识别)
{
"cartypes":[
{
"id":1,"bigimg": "img/dt-bkwl.jpg",
"cardetails": [{ "imageId02": "img/bkwl02.jpg}]
} { "id":2,"bigimg": "img/xxx.jpg", "cardetails":[{"imageId002":"img/xx.jpg"}] } ]
}
这就是简化了的json数据,可以看出这个串最外层是⼀个⼤的键为cartypes的对象,⽽它的值是json数组形式的⽐较复杂的json数据。继续
分析 [ ]的部分,可以看到,⾥⾯有两个数组元素,每个元素分别是被{ }包起来的json对象,他们的元素组成相同,再看每个元素⾥⾯包含⼏个键值对的数据,其中键cardetails的值⼜是⼀个嵌套的json数组,⾥⾯包含⼀个json对象。分析完毕。那该怎样才能(拿到数据)解析呢?
使⽤JSONObject与JSONArray
⼀般取数据有两种⽅式,看需要选择。
⽅式①:
通过 String("键")直接获取,这种⽅式只能每次获取⼀个。
⽅式②
通过构建与json对象相应的bean来获取。
我在写上⾯的例⼦时⽤到了两种⽅式,由于需要使⽤到 id,bigimg以及cardetails中的⼤部分数据,因此我在使⽤时将cardetails封装成⼀个bean,⽅便使⽤,⽽其他⽤到的⽐较少,因此就直接根据键获取值。
另外需要注意的是,JSONObject,JSONArray分别对应的是json数据的两种格式。即{"张三" : "男"} , [{ 张三" : " 男" }] ,使⽤时需要将其转
换成对应的对象。
如(⽰例):
JSONObject jsonObject = JSONObject.fromObject(json); //将json字符串转换为JSONObject
JSONArray jsonArray = JSONArray.fromObject(json); //将json字符串转换为JSONArray
还有⼀点需要指出:在取键值是始终需要根据键取值,从外到内,取内层的键的值需要先获取外层键的值,如果跨越取值会报错。
下⾯演⽰取值:
JSONObject jsonObject = JSONObject.fromObject(json); //将json字符串转化为JSONObject
String String("cartypes"); //通过getString("cartypes")取出⾥⾯的信息
JSONArray jsonArray = JSONArray.fromObject(cartypes); //将取到的cartypes对应的(⼀组)值字符串转换为JSONArray
String id= String("id"); //取id
String bigImg = String("bigimg"); //⼤图
System.out.println("bigImg:"+bigImg); //可以显⽰已经拿到bigimg的值
由于cardetails下的基本都是需要的值,⼀个⼀个取值⽐较⿇烦,因此将cardetails封装成⼀个bean 如下:
Cardetails.java
public class Cardetails {
private String imageId00;
private String imageId01;
private String imageId02;
private String imageId03;
private String imageId04;
private String carname;
private String carmatter;
private String carvolume;
private int sitnum;
private String cargearbox;
private String caremission;
private String carldone;
private String carldtwo;
private String carldthree;
private String carldfour;
//get set ⽅法以及toString⽅法略
}
到这⾥,需要将cardetails中的键全转成Cardetails中的属性,⽅法如下:
//将cardetail封装成bean
JSONArray JSONArray("cardetails");//将json字符串转化为JSONArray
JSONObject carDetailObj = JSONObject(0);//获取数组第⼀个元素
Cardetails cardetails = (Cardetails) Bean(carDetailObj, Cardetails.class);//封装成bean
System.out.println("cardetails:"+cardetails); //能获取到数据
最后附上部分代码:
public void getICarDetail(int id){
String json=null;
try {
ICarDetail(id);//这⾥既是获取上⾯json数据
} catch (Exception e) {
e.printStackTrace();
}
int jsonId=0;//json数组⾥的id值
JSONObject jsonObject = JSONObject.fromObject(json); //将json字符串转化为JSONObject
String String("cartypes");//通过getString("cartypes")取出⾥⾯的信息
JSONArray jsonArray = JSONArray.fromObject(cartypes); //将取到的cartypes对应的(⼀组)值字符串转换为JSONArray
//遍历jsonarray 数组
if(jsonArray.size()>0){
for(int i=0;i<jsonArray.size();i++){
JSONObject job = JSONObject(i);//把每⼀个对象转成json对象将数组格式的字符串转换成数组
jsonId=(("id"); //得到每个对象中的id值
if(jsonId==id){
//获取相关值
String title = String("title");
String bigImg = String("bigimg");
String repayFirst = String("repayfirst");
String endrepayone = String("endrepayone");
String endmonthone = String("endmonthone");
String marketprice = String("marketprice"); //将cardetail封装成bean
JSONArray JSONArray("cardetails");//将json字符串转化为JSONArray
JSONObject carDetailObj = JSONObject(0);//获取数组第⼀个元素
Cardetails cardetails = (Cardetails) Bean(carDetailObj, Cardetails.class);//封装成bean
//输出显⽰
System.out.println("******************");
System.out.println("jsonId:"+jsonId);
System.out.println("title:"+title);
System.out.println("bigImg:"+bigImg);
System.out.println("repayFirst:"+repayFirst);
System.out.println("endrepayone:"+endrepayone);
System.out.println("endmonthone:"+endmonthone);
System.out.println("marketprice:"+marketprice);
System.out.println("cardetails:"+cardetails);
}
到此这篇关于Java JSONObject与JSONArray对象案例详解的⽂章就介绍到这了,更多相关Java JSON
Object与JSONArray对象内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论