您好,登錄后才能下訂單哦!
/**
* 將json格式的字符串解析成Map對象 <li>
* json格式:{"name":"admin","retries":"3fff","testname"
* :"ddd","testretries":"fffffffff"}
*/
private static HashMap<String, String> toHashMap(Object object)
{
HashMap<String, String> data = new HashMap<String, String>();
// 將json字符串轉換成jsonObject
JSONObject jsonObject = JSONObject.fromObject(object);
Iterator it = jsonObject.keys();
// 遍歷jsonObject數據,添加到Map對象
while (it.hasNext())
{
String key = String.valueOf(it.next());
String value = (String) jsonObject.get(key);
data.put(key, value);
}
return data;
}
把List轉換成JSON數據:
List<UserInfo> list = new ArrayList<UserInfo>();
UserInfo user = new UserInfo(1001, "張三");
list.add(user);
list.add(user);
list.add(user);
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
把Map轉換成json, 要使用jsonObject對象:
Map<String, Object> map = new HashMap<String, Object>();
map.put("userId", 1001);
map.put("userName", "張三");
map.put("userSex", "男");
JSONObject jsonObject = JSONObject.fromObject(map);
System.out.println(jsonObject);
http://jingyan.baidu.com/article/1709ad80b780f04634c4f0d9.html
對象轉換成JSON:
UserInfo user = new UserInfo(1001,"張三");
JSONArray jsonArray = JSONArray.fromObject(user);
System.out.println( jsonArray )
;
將數組轉換為JSON:
String[] arr = {"asd","dfgd","asd","234"};
JSONArray jsonarray = JSONArray.fromObject(arr);
System.out.println(jsonarray);
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。