在阅读的过程中有任何问题,欢迎一起交流
QQ:1494713801
Java代码
- package jsonToMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- /**
- * 说明 json字符串 转成 Map/List
- * @author xss
- * @date 2013-1-18 10:22:41
- * @mail [email protected]
- */
- public class JsonToMap {
- public static void main(String[] args) {
- //JSONArray
- String jsonArrayData="[{\"a1\":\"12\",\"b1\":\"112\",\"c1\":\"132\",\"d1\":\"134\"},{\"a2\":\"12\",\"b2\":\"112\",\"c2\":\"132\",\"d2\":\"134\"},{\"a3\":\"12\",\"b3\":\"112\",\"c3\":\"132\",\"d3\":\"134\"}]";
- JSONArray jsonArray = JSONArray.fromObject(jsonArrayData);
- List<Map<String,Object>> mapListJson = (List)jsonArray;
- for (int i = 0; i < mapListJson.size(); i++) {
- Map<String,Object> obj=mapListJson.get(i);
- for(Entry<String,Object> entry : obj.entrySet()){
- String strkey1 = entry.getKey();
- Object strval1 = entry.getValue();
- System.out.println("KEY:"+strkey1+" --> Value:"+strval1+"\n");
- }
- }
- // JSONObject
- String jsonObjectData="{\"data1\":{\"a1\":\"12\",\"b1\":\"112\",\"c1\":\"132\",\"d1\":\"134\"},\"data2\":{\"a2\":\"12\",\"b2\":\"112\",\"c2\":\"132\",\"d2\":\"134\"},\"data3\":{\"a3\":\"12\",\"b3\":\"112\",\"c3\":\"132\",\"d3\":\"134\"}}";
- JSONObject jsonObject = JSONObject.fromObject(jsonObjectData);
- Map<String, Object> mapJson = JSONObject.fromObject(jsonObject);
- for(Entry<String,Object> entry : mapJson.entrySet()){
- Object strval1 = entry.getValue();
- JSONObject jsonObjectStrval1 = JSONObject.fromObject(strval1);
- Map<String, Object> mapJsonObjectStrval1 = JSONObject.fromObject(jsonObjectStrval1);
- System.out.println("KEY:"+entry.getKey()+" --> Value:"+entry.getValue()+"\n");
- for(Entry<String, Object> entry1:mapJsonObjectStrval1.entrySet()){
- System.out.println("KEY:"+entry1.getKey()+" --> Value:"+entry1.getValue()+"\n");
- }
- }
- }
- }
参考链接:http://wuniu2010.iteye.com/blog/1771934
时间: 2024-10-14 06:14:29