JSON

AndroidÖеÄJSONÏêϸ×ܽá(2)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-02-01 13:00 ÎÒÒªÆÀÂÛ( )

Ó¦ÓÃJSONObject´æ´¢MapÀàÐÍÊýÖµ£º public static JSONObject getJSON(Map map) {Iterator iter = map.entrySet().iterator();JSONObject holder = new JSONObject();while (iter.hasNext()) {Map.Entry pairs = (Ma

Ó¦ÓÃJSONObject´æ´¢MapÀàÐÍÊýÖµ£º

public static JSONObject getJSON(Map map) { Iterator iter = map.entrySet().iterator(); JSONObject holder = new JSONObject(); while (iter.hasNext()) { Map.Entry pairs = (Map.Entry) iter.next(); String key = (String) pairs.getKey(); Map m = (Map) pairs.getValue(); JSONObject data = new JSONObject(); try { Iterator iter2 = m.entrySet().iterator(); while (iter2.hasNext()) { Map.Entry pairs2 = (Map.Entry) iter2.next(); data.put((String) pairs2.getKey(), (String) pairs2 .getValue()); } holder.put(key, data); } catch (JSONException e) { Log.e("Transforming", "There was an error packaging JSON", e); } } return holder; } ÏÂÃæÊÇÏêϸµÄÀý×Ó£º

import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONObject; import android.util.Log; public class JSON { /** * »ñÈ¡"Êý×éÐÎʽ"µÄJSONÊý¾Ý£¬ * Êý¾ÝÐÎʽ£º[{"id":1,"name":"СÖí"},{"id":2,"name":"Сè"}] * @param path Íøҳ·¾¶ * @return ·µ»ØList * @throws Exception */ public static List<Map<String, String>> getJSONArray(String path) throws Exception { String json = null; List<Map<String, String>> list = new ArrayList<Map<String, String>>(); Map<String, String> map = null; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection();// ÀûÓÃHttpURLConnection¶ÔÏó,ÎÒÃÇ¿ÉÒÔ´ÓÍøÂçÖлñÈ¡ÍøÒ³Êý¾Ý. conn.setConnectTimeout(5 * 1000); // µ¥Î»ÊǺÁÃ룬ÉèÖó¬Ê±Ê±¼äΪ5Ãë conn.setRequestMethod("GET"); // HttpURLConnectionÊÇͨ¹ýHTTPЭÒéÇëÇópath·¾¶µÄ£¬ËùÒÔÐèÒªÉèÖÃÇëÇó·½Ê½,¿ÉÒÔ²»ÉèÖã¬ÒòΪĬÈÏΪGET if (conn.getResponseCode() == 200) {// ÅжÏÇëÇóÂëÊÇ·ñÊÇ200Â룬·ñÔòʧ°Ü InputStream is = conn.getInputStream(); // »ñÈ¡ÊäÈëÁ÷ byte[] data = readStream(is); // °ÑÊäÈëÁ÷ת»»³É×Ö·ûÊý×é json = new String(data); // °Ñ×Ö·ûÊý×éת»»³É×Ö·û´® //Êý¾ÝÐÎʽ£º[{"id":1,"name":"СÖí","age":22},{"id":2,"name":"Сè","age":23}] JSONArray jsonArray = new JSONArray(json); //Êý¾ÝÖ±½ÓΪһ¸öÊý×éÐÎʽ£¬ËùÒÔ¿ÉÒÔÖ±½Ó ÓÃandroidÌṩµÄ¿ò¼ÜJSONArray¶ÁÈ¡JSONÊý¾Ý£¬×ª»»³ÉArray for (int i = 0; i < jsonArray.length(); i++) { JSONObject item = jsonArray.getJSONObject(i); //ÿÌõ¼Ç¼ÓÖÓɼ¸¸öObject¶ÔÏó×é³É int id = item.getInt("id"); // »ñÈ¡¶ÔÏó¶ÔÓ¦µÄÖµ String name = item.getString("name"); map = new HashMap<String, String>(); // ´æ·Åµ½MAPÀïÃæ map.put("id", id + ""); map.put("name", name); list.add(map); } } // ***********²âÊÔÊý¾Ý****************** for (Map<String, String> list2 : list) { String id = list2.get("id"); String name = list2.get("name"); Log.i("abc", "id:" + id + " | name:" + name); } return list; } /** * »ñÈ¡"¶ÔÏóÐÎʽ"µÄJSONÊý¾Ý£¬ * Êý¾ÝÐÎʽ£º{"total":2,"success":true,"arrayData":[{"id":1,"name":"СÖí"},{"id":2,"name":"Сè"}]} * @param path Íøҳ·¾¶ * @return ·µ»ØList * @throws Exception */ public static List<Map<String, String>> getJSONObject(String path) throws Exception { List<Map<String, String>> list = new ArrayList<Map<String, String>>(); Map<String, String> map = null; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection();// ÀûÓÃHttpURLConnection¶ÔÏó,ÎÒÃÇ¿ÉÒÔ´ÓÍøÂçÖлñÈ¡ÍøÒ³Êý¾Ý. conn.setConnectTimeout(5 * 1000); // µ¥Î»ÊǺÁÃ룬ÉèÖó¬Ê±Ê±¼äΪ5Ãë conn.setRequestMethod("GET"); // HttpURLConnectionÊÇͨ¹ýHTTPЭÒéÇëÇópath·¾¶µÄ£¬ËùÒÔÐèÒªÉèÖÃÇëÇó·½Ê½,¿ÉÒÔ²»ÉèÖã¬ÒòΪĬÈÏΪGET if (conn.getResponseCode() == 200) {// ÅжÏÇëÇóÂëÊÇ·ñÊÇ200Â룬·ñÔòʧ°Ü InputStream is = conn.getInputStream(); // »ñÈ¡ÊäÈëÁ÷ byte[] data = readStream(is); // °ÑÊäÈëÁ÷ת»»³É×Ö·ûÊý×é String json = new String(data); // °Ñ×Ö·ûÊý×éת»»³É×Ö·û´® //Êý¾ÝÐÎʽ£º{"total":2,"success":true,"arrayData":[{"id":1,"name":"СÖí"},{"id":2,"name":"Сè"}]} JSONObject jsonObject=new JSONObject(json); //·µ»ØµÄÊý¾ÝÐÎʽÊÇÒ»¸öObjectÀàÐÍ£¬ËùÒÔ¿ÉÒÔÖ±½Óת»»³ÉÒ»¸öObject int total=jsonObject.getInt("total"); Boolean success=jsonObject.getBoolean("success"); Log.i("abc", "total:" + total + " | success:" + success); //²âÊÔÊý¾Ý JSONArray jsonArray = jsonObject.getJSONArray("arrayData");//ÀïÃæÓÐÒ»¸öÊý×éÊý¾Ý£¬¿ÉÒÔÓÃgetJSONArray»ñÈ¡Êý×é for (int i = 0; i < jsonArray.length(); i++) { JSONObject item = jsonArray.getJSONObject(i); // µÃµ½Ã¿¸ö¶ÔÏó int id = item.getInt("id"); // »ñÈ¡¶ÔÏó¶ÔÓ¦µÄÖµ String name = item.getString("name"); map = new HashMap<String, String>(); // ´æ·Åµ½MAPÀïÃæ map.put("id", id + ""); map.put("name", name); list.add(map); } } // ***********²âÊÔÊý¾Ý****************** for (Map<String, String> list2 : list) { String id = list2.get("id"); String name = list2.get("name"); Log.i("abc", "id:" + id + " | name:" + name); } return list; } /** * »ñÈ¡ÀàÐ͸´ÔÓµÄJSONÊý¾Ý *Êý¾ÝÐÎʽ£º {"name":"СÖí", "age":23, "content":{"questionsTotal":2, "questions": [ { "question": "what's your name?", "answer": "СÖí"},{"question": "what's your age", "answer": "23"}] } } * @param path Íøҳ·¾¶ * @return ·µ»ØList * @throws Exception */ public static List<Map<String, String>> getJSON(String path) throws Exception { List<Map<String, String>> list = new ArrayList<Map<String, String>>(); Map<String, String> map = null; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection();// ÀûÓÃHttpURLConnection¶ÔÏó,ÎÒÃÇ¿ÉÒÔ´ÓÍøÂçÖлñÈ¡ÍøÒ³Êý¾Ý. conn.setConnectTimeout(5 * 1000); // µ¥Î»ÊǺÁÃ룬ÉèÖó¬Ê±Ê±¼äΪ5Ãë conn.setRequestMethod("GET"); // HttpURLConnectionÊÇͨ¹ýHTTPЭÒéÇëÇópath·¾¶µÄ£¬ËùÒÔÐèÒªÉèÖÃÇëÇó·½Ê½,¿ÉÒÔ²»ÉèÖã¬ÒòΪĬÈÏΪGET if (conn.getResponseCode() == 200) {// ÅжÏÇëÇóÂëÊÇ·ñÊÇ200Â룬·ñÔòʧ°Ü InputStream is = conn.getInputStream(); // »ñÈ¡ÊäÈëÁ÷ byte[] data = readStream(is); // °ÑÊäÈëÁ÷ת»»³É×Ö·ûÊý×é String json = new String(data); // °Ñ×Ö·ûÊý×éת»»³É×Ö·û´® /*Êý¾ÝÐÎʽ£º {"name":"СÖí", "age":23, "content":{"questionsTotal":2, "questions": [ { "question": "what's your name?", "answer": "СÖí"},{"question": "what's your age", "answer": "23"}] } } */ JSONObject jsonObject=new JSONObject(json); //·µ»ØµÄÊý¾ÝÐÎʽÊÇÒ»¸öObjectÀàÐÍ£¬ËùÒÔ¿ÉÒÔÖ±½Óת»»³ÉÒ»¸öObject String name=jsonObject.getString("name"); int age=jsonObject.getInt("age"); Log.i("abc", "name:" + name + " | age:" + age); //²âÊÔÊý¾Ý JSONObject contentObject=jsonObject.getJSONObject("content"); //»ñÈ¡¶ÔÏóÖеĶÔÏó String questionsTotal=contentObject.getString("questionsTotal"); //»ñÈ¡¶ÔÏóÖеÄÒ»¸öÖµ Log.i("abc", "questionsTotal:" + questionsTotal); //²âÊÔÊý¾Ý JSONArray contentArray=contentObject.getJSONArray("questions"); //»ñÈ¡¶ÔÏóÖеÄÊý×é for (int i = 0; i < contentArray.length(); i++) { JSONObject item = contentArray.getJSONObject(i); // µÃµ½Ã¿¸ö¶ÔÏó String question = item.getString("question"); // »ñÈ¡¶ÔÏó¶ÔÓ¦µÄÖµ String answer = item.getString("answer"); map = new HashMap<String, String>(); // ´æ·Åµ½MAPÀïÃæ map.put("question", question); map.put("answer", answer); list.add(map); } } // ***********²âÊÔÊý¾Ý****************** for (Map<String, String> list2 : list) { String question = list2.get("question"); String answer = list2.get("answer"); Log.i("abc", "question:" + question + " | answer:" + answer); } return list; } /** * °ÑÊäÈëÁ÷ת»»³É×Ö·ûÊý×é * @param inputStream ÊäÈëÁ÷ * @return ×Ö·ûÊý×é * @throws Exception */ public static byte[] readStream(InputStream inputStream) throws Exception { ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inputStream.read(buffer)) != -1) { bout.write(buffer, 0, len); } bout.close(); inputStream.close(); return bout.toByteArray(); } }

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • JSONÊý¾ÝÖÐÓÐnullµ¼ÖÂÊý¾Ý¼ÓÔØʧ°ÜµÄ½â¾ö°ì·¨

    JSONÊý¾ÝÖÐÓÐnullµ¼ÖÂÊý¾Ý¼ÓÔØʧ°ÜµÄ½â¾ö°ì·¨

    2017-02-01 12:04

  • json½Ì³ÌϵÁУ¨5£©-json´íÎó½âÎönet.sf.ezmorph.bean.MorphDynaBean cannot

    json½Ì³ÌϵÁУ¨5£©-json´íÎó½âÎönet.sf.ezmorph.bean.MorphDynaBean

    2017-01-31 16:02

  • JSON¶ÔÏó Ïê½â¼°ÊµÀý´úÂë

    JSON¶ÔÏó Ïê½â¼°ÊµÀý´úÂë

    2017-01-31 08:02

  • ³ÌÐòÔ±½Ì³Ì

    ³ÌÐòÔ±½Ì³Ì

    2017-01-30 18:02

ÍøÓѵãÆÀ
æ