package com.test;

import java.util.ArrayList;
import java.util.HashMap;
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
 * 
 */
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\":{\"X\":\"12\",\"Y\":\"112\"},\"data2\":{\"X\":\"12\",\"Y\":\"112\"}}";
		String jsonObjectData="{\"CSM\":{\"MAP_ID1\":{\"X\":\"12\",\"Y\":\"112\"},\"MAP_ID11\":{\"X\":\"12\",\"Y\":\"112\"}},\"DM\":{\"MAP_ID2\":{\"X\":\"12\",\"Y\":\"112\"},\"MAP_ID22\":{\"X\":\"12\",\"Y\":\"112\"}}}";
		
		JSONObject jsonObject = JSONObject.fromObject(jsonObjectData);
		Map<String, List> stage_ids = new HashMap<String, List>()	;
		Map<String, Object> mapJson = JSONObject.fromObject(jsonObject);
		
        for(Entry<String,Object> entry : mapJson.entrySet()){
            Object strval1 = entry.getValue();
            System.out.println("第一层 KEY:"+entry.getKey()+"  -->  Value:"+entry.getValue()+"\n");            
            JSONObject jsonObjectStrval1 = JSONObject.fromObject(strval1);            
            Map<String, Object> mapJsonObjectStrval1 = JSONObject.fromObject(jsonObjectStrval1);
            List<String> ids = new ArrayList<String>();
            
	   for(Entry<String, Object> entry1:mapJsonObjectStrval1.entrySet()){
            	System.out.println("第二层 KEY:"+entry1.getKey()+"  -->  Value:"+entry1.getValue()+"\n");
            	ids.add(entry1.getKey());
                Object strval3 = entry1.getValue();
            	JSONObject jsonObjectStrval3 = JSONObject.fromObject(strval3);
            	Map<String, Object> mapJsonObjectStrval3 = JSONObject.fromObject(jsonObjectStrval3);
            	
		    for(Entry<String, Object> entry3:mapJsonObjectStrval3.entrySet()){
            		 System.out.println("第三层 KEY:"+entry3.getKey()+"  -->  Value:"+entry3.getValue()+"\n");
                 	
            	 }
            }
            stage_ids.put(entry.getKey(), ids);
            System.out.println("-------------------------------------");
            System.out.println(stage_ids.get("CSM").toString());
            System.out.println("-------------------------------------");
        }
      
	}

}