首页 > 代码库 > java json字符串转成 Map或List

java json字符串转成 Map或List

 

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 wuniu2010@126.com
 */
public class JsonToMap {

    public static void main(String[] args) {
        
        //JSONArray
        String jsonArrayData="http://www.mamicode.com/[{/"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="http://www.mamicode.com/{/"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");
            }
            
        }
    }

}

 

org.json  jar包环境下:(json所需要的jar包:点我下载)

import java.util.Iterator;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
 * @date 2013-2-1 10:36:22
 * @author xss
 * @说明 org.json jar包环境下 遍历 不知道外层key的json字符串
 */
public class JsonTest {

    public static void main(String[] args) {
        try {
            String json = "{\"1293142@1098820@1211867_1083940_1293396_1148650\":[{\"FS_ID\":\"1293396\",\"SX_MC\":\"李四\",\"YFS_ID\":\"1293142\",\"SZ_ID\":\"1148650\",\"SX_Z\":\"803932\"}],\"1293142@1098820@1211867_1083940_1293396_1097084\":[{\"FS_ID\":\"1293396\",\"SX_MC\":\"李四\",\"YFS_ID\":\"1293142@1084106@1103027\",\"SZ_ID\":\"1097084\",\"SX_Z\":\"124\"}],\"1293142@1098820@1211867_1083940_1293331_1148650\":[{\"FS_ID\":\"1293331\",\"SX_MC\":\"张三\",\"YFS_ID\":\"1293142\",\"SZ_ID\":\"1148650\",\"SX_Z\":\"1005240\"}],\"1293142@1098820@1211867_1083940_1293331_1097084\":[{\"FS_ID\":\"1293331\",\"SX_MC\":\"张三\",\"YFS_ID\":\"1293142@1084106@1103027\",\"SZ_ID\":\"1097084\",\"SX_Z\":\"204\"}],\"1293142@1098820@1211867_1083940_1284806_1148650\":[{\"FS_ID\":\"1284806\",\"SX_MC\":\"王小二\",\"YFS_ID\":\"1293142\",\"SZ_ID\":\"1148650\",\"SX_Z\":\"513475\"}],\"1293142@1098820@1211867_1083940_1284806_1097084\":[{\"FS_ID\":\"1284806\",\"SX_MC\":\"王小二\",\"YFS_ID\":\"1293142@1084106@1103027\",\"SZ_ID\":\"1097084\",\"SX_Z\":\"145\"}],\"1293142@1098820@1211867_1083940_1293461_1148650\":[{\"FS_ID\":\"1293461\",\"SX_MC\":\"朱重八\",\"YFS_ID\":\"1293142\",\"SZ_ID\":\"1148650\",\"SX_Z\":\"339040\"}],\"1293142@1098820@1211867_1083940_1293461_1097084\":[{\"FS_ID\":\"1293461\",\"SX_MC\":\"朱重八\",\"YFS_ID\":\"1293142@1084106@1103027\",\"SZ_ID\":\"1097084\",\"SX_Z\":\"52\"}]}";
            JSONObject jsonObj = new JSONObject(json);

            //方式二
            Iterator it = jsonObj.keys();
            for (int j = 0; it.hasNext(); j++) {
                String str = it.next().toString();
                System.out.println(str);

                JSONArray array = new JSONArray(jsonObj.get(str).toString());
                System.out.println(array);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject jons = array.getJSONObject(i);
                    System.out.println(jons.get("SX_MC"));
                }
            }
            System.out.println("tep2");
            
            //方式1
            Set<String> set = jsonObj.keySet();
            for (String str : set) {
                System.out.println(str);
                JSONArray array = new JSONArray(jsonObj.get(str).toString());
                System.out.println(array);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject jons = array.getJSONObject(i);
                    System.out.println(jons.get("SX_MC"));
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

 

已发表于Iteye

 

java json字符串转成 Map或List