JSON转换
JSON转换
1.JSON转为List<Map<String, Object>>
String costStatus="[{\"id\":24489,\"itemStatus\":1},{\"id\":24529,\"itemStatus\":1}]";
List<Map<String, Object>> jsonMap = (List<Map<String, Object>>) JSONArray.parse(costStatus);
if(!org.springframework.util.StringUtils.isEmpty(jsonMap)){
for (Map<String, Object> map:jsonMap) {
String id = (String )map.get("id");
String itemStatus = (String)map.get("itemStatus");
}
}
**2.JSON转为subObject **
String jsonString="[{"itemName":"物业","itemVal":"7317.28"},{"itemName":"租金","itemVal":"38512"}]";
JSONArray array = JSONArray.parseArray(jsonString);
if (!StringUtils.isEmpty(array)) {
float total = 0f;
for (int i = 0; i < array.size(); i++) {
JSONObject subObject = (JSONObject) array.get(i);
String itemName = (String) subObject.get("itemName");
String itemVal = (String) subObject.get("itemVal");
}
}
3.依赖包
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;