複雜的JSON格式資料解析取值
{"0":[{"name":"品牌","value":"1,2,3,4"}],"1":[{"name":"材質","value":""},{"name":"風格","value":""}],"2":[{"name":"型別","value":"運動,休閒,登山"}],"3":[{"name":"適用季節","value":"春,夏,秋,冬"}],"4":[{"name":"生產日期","value":""},{"name":"結束日期","value":""}]}
以上這種格式獲取value的值
public ResponseMsg search(String str) {
String categoryName = "%"+str+"%";
List<ProCategoryEntity> proCategoryEntity = proMallService.search(categoryName);
for (ProCategoryEntity proCategoryEntity2 : proCategoryEntity) {
str1 = proCategoryEntity2.getCategoryAttr();
}
JSONObject object = JSONObject.parseObject(str1);
JSONArray parseArray = JSONObject.parseArray(JSONObject.parseObject(str1).get("0").toString());
for(int j=0;j<parseArray.size();j++){
JSONObject parseObject = JSONObject.parseObject(parseArray.get(j).toString());
String s =(String) parseObject.get("value");
標紅這段程式碼可以縮減成{
String s =(String) JSONObject.parseObject(JSONObject.parseArray(JSONObject.parseObject(str1).get("0").toString()).get(i).toString()).get("value");這段程式碼
}
String[] split = s.split(",");
for(int t=0;t<split.length;t++){
System.out.println(split [t]);
}
}