public static void main(String[] args){
String temp="{'data':{'a':[{'b1':'bb1','c1':'cc1'},{'b2':'bb2','c2':'cc2'}]}}"
JSONObject jodata =JSONObject.fromObject(temp)
JSONObject joa =JSONObject.fromObject(jodata.get("data").toString())
JSONArray ja=JSONArray.fromObject(joa.get("a"))
for(int i=0i<ja.size()i++){
JSONObject o=ja.getJSONObject(i)
if(o.get("b1")!=null){
System.out.println(o.get("b1"))
}
if(o.get("c1")!=null){
System.out.println(o.get("c1"))
}
if(o.get("b2")!=null){
System.out.println(o.get("b2"))
}
if(o.get("c2")!=null){
System.out.println(o.get("c2"))
}
}
}
注:要包含两个jar包ezmorph-1.0.6.jar和json-lib-2.2.2-jdk15.jar,jar包在附件中
JSON转成java集合简单步骤:1,加载:JSONArray jarr=JSONArray.fromObject(json字符串名)2,转换:List<类型>list=(List<类型>)JSONArray.toColleaction(jarr,类型.class)
JSON转成java对象步骤,1,同上
2,类型如Emp(员工类)
Emp e=(Emp)JSONObject.toBean(jarr,Emp.class)
前提需要导入common的一系列的类库,大概有6个jar包吧。
后面的需要你自己找你需要的某个字符串了。不知道是不是你所说的意思。
你好:
后台拆分json
private String interactPrizeAll //json 使用字符串来接收方法中的代码:
Gson gson = new Gson()
InteractPrize interactPrize =new InteractPrize()
//gson用泛型 转List数组 多个对象
List<InteractPrize > interactPrizeList = gson.fromJson(interactPrizeAll, new TypeToken<List<InteractPrize>>(){}.getType()) //TypeToken,它是gson提供的数据类型转换器,可以支持各种数据集合类型转换
for(int i = 0 i < interactPrizeList.size() i++)
{
interactPrize = interactPrizeList.get(i)//获取每一个对象
}
这一种方法是转单个对象时使用的
//gson 转对象 单个对象
//interactPrize = gson.fromJso(interactPrizeAll,InteractPrize.class)
这个方法是我后台拼的json往前台传的方法
jsonStrAll.append("{"+ "\""+"catid"+"\""+":"+"\""+c.getCatid()+"\""+","+"\""+"catname"+"\""+":"+"\""+c.getCatname()+"\""+","+ "\""+"catdesc"+"\""+":"+"\""+c.getCatdesc()+"\""+","+"\""+"showinnav"+"\""+":"+"\""+c.getShowinnav()+"\""+","+"\""+"sortorder"+"\""+":"+"\""+c.getSortorder()+"\""+","+"level:"+"\""+"0"+"\""+",parent:"+"\""+"0"+"\""+",isLeaf:true,expanded:false,"+"loaded:true},")
你自己挑着用吧!