数据格式
"label_ids": " [{\"brand\":\"小破狗狗\",\"clothing\":\"shirt\"},{\"brand\":\"全心全意陪我\",\"clothing\":\"coat\"},{\"brand\":\"所迫\",\"clothing\":\"one_piece_dress\"},{\"brand\":\"图我\",\"clothing\":\"skirt\"},{\"brand\":\"扑上去\",\"clothing\":\"bag\"},{\"brand\":\"是相濡以沫\",\"clothing\":\"decoration\"},{\"brand\":\"随心所欲随心所欲\",\"clothing\":\"hat\"},{\"brand\":\"其中一种\",\"clothing\":\"trousers\"},{\"brand\":\"破狗狗\",\"clothing\":\"others\"}] ",
需要解析label_ids 字段
首先String 接收
然后是一个将数据转户成成bean类型集合的公共方法
public static List<? extends Object> analysisArray(String json, Type type) { List<Object> list = new ArrayList<>(); try { JSONArray jsonArray = new JSONArray(json); for (int i = 0; i < jsonArray.length(); i++) { Object o = new Gson().fromJson(String.valueOf(jsonArray.get(i)), type); list.add(o); } } catch (JSONException e) { e.printStackTrace(); } return list; }
调用的时候 只需要先创建一个bean类型的实体,因为json是数组 所以返回的是集合 需要写一个List<Bean> 用来接收解析之后的数据
List<GoodsLabIds> goodLabIdLists = new ArrayList<>();
goodLabIdLists.addAll((List<GoodsLabIds>) JsonUtil.analysisArray(label_ids, GoodsLabIds.class));
goodLabIdLists.addAll((List<GoodsLabIds>) JsonUtil.analysisArray(label_ids, GoodsLabIds.class));