主要用到了 net.sf.json.JSONObject类
需要用到的jar包 : jar包下载地址
1 package test; 2 3 import java.io.BufferedReader; 4 import java.io.InputStream; 5 import java.io.InputStreamReader; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 import java.util.HashMap; 9 import java.util.Iterator; 10 import java.util.List; 11 import java.util.Map; 12 import java.util.Map.Entry; 13 14 import net.sf.json.JSONObject; 15 16 17 public class Test { 18 19 private static HashMap<String, String> toHashMap(Object object){ 20 HashMap<String, String> data = new HashMap<String, String>(); 21 JSONObject jsonObject = JSONObject.fromObject(object); 22 Iterator it = jsonObject.keys(); 23 while(it.hasNext()){ 24 String key = String.valueOf(it.next()); 25 String value = (String)jsonObject.get(key); 26 data.put(key, value); 27 } 28 return data; 29 } 30 public static void main(String[] args) { 31 32 String jsonString = "{"name":"admin","retries":"3fff","testname" :"ddd","testretries":"fffffffff"} "; 33 HashMap<String, String> mp = toHashMap(jsonString); 34 System.out.println(mp.get("name")); 35 System.out.println(mp.get("retries")); 36 System.out.println(mp.get("testname")); 37 } 38 }