fastjson的JSONArray和JSONObject
在做JSON反序列化的时候,我们可能经常传递一个class对象来获取对象的示例。但有的时候,可能并不存在这样的class对象:
另外换个角度解决这些问题就是:不定义任何类,直接通过JSONArray和JSONObject来访问。
通过组合使用JSONArray和JSONObject,我们就可以访问任何反序列化出来的JSON对象,而不需要对象的类信息。 示例: importcom.alibaba.fastjson.JSON; importcom.alibaba.fastjson.JSONArray; publicclassParseJSONObject{ publicstaticvoidmain(Stringargs[]){ Stringjson="[{"id":1,"type":"cycle","attribute":{"center":"(10.4,123.345)","radius":67.4}},{"id":2,"type":"polygon","attribute":[{"vertex":"(10.4,133.345)"},{"vertex":"(10.4,143.345)"}]}]"; JSONArrayarray=JSON.parseArray(json); System.out.println(array.getJSONObject(0).getJSONObject("attribute").get("center")); System.out.println(array.getJSONObject(1).getJSONArray("attribute").getJSONObject(1).get("vertex")); } } 输出为: (10.4,123.345) (10.4,143.345) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |