解析奇葩json。
先看一下这个json有多奇葩:


catList作为一个JSONArray,每个item下的子item个数不确定,且都是用数字作为key。(还有很多重复的key)
网上页找了一些动态key的做法,但是和我这个json结构都不一样,只能根据自己的实际情况来获取动态key了。
首先,因为不是正规json格式,用retrofit获取结果的时候,就不能直接封装为bean对象了,直接拿到字符串自己来解析封装。
先将字符串转化为JSONObject
JSONObject jsonObject = JSONObject.parseObject(goodsSearchVO);
拿到“data” key下的JSONObject
JSONObject dataJSON = jsonObject.getJSONObject("data");
拿到catList的JSONArray
JSONArray catListJSON = dataJSON.getJSONArray("catList");
现在开始解析这个catListJSON了。
参考了一下本篇文章:fastjson获取key值
LinkedHashMap<String, String> jsonMap = JSON.parseObject(catListJSON.get(i).toString(),new TypeReference<LinkedHashMap<String, String>>(){});
for (Map.Entry<String, String> entry : jsonMap.entrySet()) {
}
}
这样,能拿到catListJSON里所有的key和value。因为用的LinkedHashMap,重复的不会加入到jsonMap中。
完整代码:
List<GoodsCatItemSearchVO> resultList= new ArrayList<>();
LinkedHashMap<String, String> jsonMap = JSON.parseObject(catListJSON.get(i).toString(),new TypeReference<LinkedHashMap<String, String>>(){});
for (Map.Entry<String, String> entry : jsonMap.entrySet()) {
if("cat_name".equals(entry.getKey()) || "cat_id".equals(entry.getKey())){
continue;
}
JSONArray catItemListJSON = JSONArray.parseArray(entry.getValue());
for (int q = 0; q < catItemListJSON.size(); q++) {
JSONObject string = catItemListJSON.getJSONObject(q);
GoodsCatItemSearchVO item = JSON.toJavaObject(string,GoodsCatItemSearchVO.class);
//Log.v("itt","=="+item.getCat_name());
resultList.add(item);
}
}
因为需求要的是数字key里的内容,所有key为cat_name和cat_id时,continue循环。
entry.getValue(),得到是[{"155":[{"cat_name":"葡萄酒",这样的JSONArray字符串,所以需要将它转化为
JSONArray catItemListJSON = JSONArray.parseArray(entry.getValue());
然后取出每一项item的字符串
JSONObject string = catItemListJSON.getJSONObject(q);
再用toJavaObject方法,将字符串转化为GoodsCatItemSearchVO对象,再add到resultList里。
整个catList里的数据就算拿到了。。
接下来,还有另一种奇葩json。

这一种看起来复杂,但是还没有上面的catList复杂。
首先它是{}包裹的,是一个Object,所以可以免了最外层的for循环。
JSONObject propsDataJSON = dataJSON.getJSONObject("propsData");
List<GoodsPropsItemSearchVO> propsDataList= new ArrayList<>();
if(propsDataJSON != null) {
//方法二---有序
LinkedHashMap<String, String> jsonMap = JSON.parseObject(propsDataJSON.toString(), new TypeReference<LinkedHashMap<String, String>>() {
});
for (Map.Entry<String, String> entry : jsonMap.entrySet()) {
Log.v("pr2","key=="+entry.getKey());
//Log.v("itt", "ppp==" + entry.getKey() + ":" + entry.getValue());
JSONArray propsItemListJSON = JSONArray.parseArray(entry.getValue());
for (int q = 0; q < propsItemListJSON.size(); q++) {
JSONObject string = propsItemListJSON.getJSONObject(q);
GoodsPropsItemSearchVO item = JSON.toJavaObject(string, GoodsPropsItemSearchVO.class);
Log.v("itt2", "==" + item.getProp_value());
propsDataList.add(item);
}
}
}
会了第一种,第二种就好做了。
另外,导的包是:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
关于去重排序:
可以用LinkedHashSet添加,然后转arraylist
也可以用arraylist添加,再用List<GoodsCatItemSearchVO> a= new ArrayList<>(new LinkedHashSet<>(resultList));去重
另外可以参考文章:android解析json时动态获取key
Java实现Json字符串与Object对象相互转换的方式总结
使用fastjson 进行jsonObject转实体类对象
使用LinkedHashSet给ArrayList去重并保持顺序
关于JSON的一些小知识点:
对象转字符串:
String json = com.alibaba.fastjson.JSON.toJSON(shopsBean).toString();
字符串转对象:
User user1 = com.alibaba.fastjson.JSON.parseObject(json,User.class);
字符串转JSONObject:
import com.alibaba.fastjson.JSONObject;
JSONObject jsonObject = JSONObject.parseObject(json);
另外一种:
import org.json.JSONObject;
JSONObject j = null;
try {
j = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
网友评论