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               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行业卡(新)


你可能感兴趣的:(java)