java json字串轉成 Map或List

來源:互聯網
上載者:User

標籤:tle   http   .json   author   getjson   public   class   方式   blank   

 

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");            }                    }    }}

 

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 = "{\"[email protected]@1211867_1083940_1293396_1148650\":[{\"FS_ID\":\"1293396\",\"SX_MC\":\"李四\",\"YFS_ID\":\"1293142\",\"SZ_ID\":\"1148650\",\"SX_Z\":\"803932\"}],\"[email protected]@1211867_1083940_1293396_1097084\":[{\"FS_ID\":\"1293396\",\"SX_MC\":\"李四\",\"YFS_ID\":\"[email protected]@1103027\",\"SZ_ID\":\"1097084\",\"SX_Z\":\"124\"}],\"[email protected]@1211867_1083940_1293331_1148650\":[{\"FS_ID\":\"1293331\",\"SX_MC\":\"張三\",\"YFS_ID\":\"1293142\",\"SZ_ID\":\"1148650\",\"SX_Z\":\"1005240\"}],\"[email protected]@1211867_1083940_1293331_1097084\":[{\"FS_ID\":\"1293331\",\"SX_MC\":\"張三\",\"YFS_ID\":\"[email protected]@1103027\",\"SZ_ID\":\"1097084\",\"SX_Z\":\"204\"}],\"[email protected]@1211867_1083940_1284806_1148650\":[{\"FS_ID\":\"1284806\",\"SX_MC\":\"王小二\",\"YFS_ID\":\"1293142\",\"SZ_ID\":\"1148650\",\"SX_Z\":\"513475\"}],\"[email protected]@1211867_1083940_1284806_1097084\":[{\"FS_ID\":\"1284806\",\"SX_MC\":\"王小二\",\"YFS_ID\":\"[email protected]@1103027\",\"SZ_ID\":\"1097084\",\"SX_Z\":\"145\"}],\"[email protected]@1211867_1083940_1293461_1148650\":[{\"FS_ID\":\"1293461\",\"SX_MC\":\"朱重八\",\"YFS_ID\":\"1293142\",\"SZ_ID\":\"1148650\",\"SX_Z\":\"339040\"}],\"[email protected]@1211867_1083940_1293461_1097084\":[{\"FS_ID\":\"1293461\",\"SX_MC\":\"朱重八\",\"YFS_ID\":\"[email protected]@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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.