本文目录一览:
怎么把json字符串转换成map
在升级QQ登录到OAuth2.0时,其返回的是一个json形式的字符串,将其转换成Map
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
或
Gson gson = new Gson();
String json 如下;
{ "ret":0, "msg":"", "nickname":"xxx", "figureurl":"", "figureurl_1":"", "figureurl_2":"", "gender":"xxx", "vip":"0", "level":"0", "is_yellow_year_vip":"0" }
Map infoMap = gson.fromJson(json, new TypeTokenMapString, String(){}.getType());
怎么把json字符串转换map?
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import net.sf.json.JSONObject;
public MapString, String toMap(Object object)
{MapString, String data = new HashMapString, String();
// 将json字符串转换成jsonObject
JSONObject jsonObject = JSONObject.fromObject(object);
Iterator ite = jsonObject.keys();
// 遍历jsonObject数据,添加到Map对象
while (ite.hasNext())
{String key = ite.next().toString();
String value = jsonObject.get(key).toString();
data.put(key, value);
// 或者直接将 jsonObject赋值给Map
// data = jsonObject;
return data;
}
JAVA中json字符串如何转化为map对象获取数据?
我们需要先把json字符串转化为net.sf.json.JSONObject对象,java中这样就可以完成json字符串到Map的转换了。
1.将数组转换为JSON:String[] arr = {"asd","dfgd","asd","234"};JSONArray jsonarray = JSONArray.fromObject(arr);System.out.println(jsonarray);
2.对象转换成JSON:UserInfo user = new UserInfo(1001,"张三");JSONArray jsonArray = JSONArray.fromObject(user);System.out.println( jsonArray );
3.把Map转换成json, 要使用jsonObject对象:MapString, Object map = new HashMapString, Object();map.put("userId", 1001);map.put("userName", "张三");map.put("userSex", "男");JSONObject jsonObject = JSONObject.fromObject(map);System.out.println(jsonObject);
4.把List转换成JSON数据:ListUserInfo list = new ArrayListUserInfo();UserInfo user = new UserInfo(1001, "张三");list.add(user);list.add(user);list.add(user);JSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray);
5.
JAVA中json字符串如何转化为map对象,获取数据?
我们需要先把json字符串转化为net.sf.json.JSONObject对象,java中这样就可以完成json字符串到Map的转换了。
1.将数组转换为JSON:String[] arr = {"asd","dfgd","asd","234"};JSONArray jsonarray = JSONArray.fromObject(arr);System.out.println(jsonarray);
2.对象转换成JSON:UserInfo user = new UserInfo(1001,"张三");JSONArray jsonArray = JSONArray.fromObject(user);System.out.println( jsonArray );
3.把Map转换成json, 要使用jsonObject对象:MapString, Object map = new HashMapString, Object();map.put("userId", 1001);map.put("userName", "张三");map.put("userSex", "男");JSONObject jsonObject = JSONObject.fromObject(map);System.out.println(jsonObject);
4.把List转换成JSON数据:ListUserInfo list = new ArrayListUserInfo();UserInfo user = new UserInfo(1001, "张三");list.add(user);list.add(user);list.add(user);JSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray);
5.