/**
* 将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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。