gson复杂json解析

{"resultCode":"0","userInfoVos":[{"userInfoVo":{"msisdn":"1787660589","packageCode":"132","packageName":"动力100行业卡(新)","openTime":"2017-01-12","productName":"无线宽带专网","state":"正常使用","apnId":"1","apnName":"CMNET","packageStartTime":"2017-01-12","packageEndTime":"2099-12-31","ipAddr":"-1","activateTime":"2017-01-12"}},{"userInfoVo":{"msisdn":"1787660589","packageCode":"PV.DDDRoam","packageName":"省际漫游(限国内)","openTime":"2017-01-12","productName":"无线宽带专网","state":"正常使用","apnId":"1","apnName":"CMNET","packageStartTime":"2017-01-12","packageEndTime":"2037-01-01","ipAddr":"-1","activateTime":"2017-01-12"}}]}


import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Test {    
    public static void main(String[] args) throws Exception {

String aa="{\"resultCode\":\"0\",\"userInfoVos\":[{\"userInfoVo\":{\"msisdn\":\"17876607589\",\"packageCode\":\"1032\",\"packageName\":\"动力100行业卡(新)\"}}]}";
        JsonParser parser=new JsonParser();  //创建JSON解析器
        JsonObject object=(JsonObject) parser.parse(aa); //创建JsonObject对象
        JsonArray array=object.get("userInfoVos").getAsJsonArray();    //得到为json的数组
        for(int i=0;i<array.size();i++){
              JsonObject subObject=array.get(0).getAsJsonObject();
              JsonObject userInfoVo=subObject.get("userInfoVo").getAsJsonObject();
                     String packageCode= userInfoVo.get("packageCode").getAsString();
                     String packageName= userInfoVo.get("packageName").getAsString();
//                     String openTime= userInfoVo.get("openTime").getAsString();
//                     String state= userInfoVo.get("state").getAsString();
//                     String apnName= userInfoVo.get("apnName").getAsString();
//                     String packageStartTime= userInfoVo.get("packageStartTime").getAsString();
//                     String packageEndTime= userInfoVo.get("packageEndTime").getAsString();
//                     String activateTime= userInfoVo.get("activateTime").getAsString();
                     System.out.println(packageCode);
                     System.out.println(packageName);
//                     System.out.println(openTime);
            }
    }
}

结果:

1032
动力100行业卡(新)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值