解析map中对象的字段

103 阅读1分钟

image.png

route是一个Map<*,*>,需要解析data中的id

解析如下:

if (null != route) {
    Map<?, ?> routeData = (Map<?, ?>) route.get("data");
    if (routeData != null) {
        JSONObject jsonObject = GsonUtil.toJSONObject(routeData);
        Object host_id = jsonObject.optInt("host_id");
    }
}

GsonUtilcn.colorv.net.retrofit包下的

/**
 * map to JSONObject
 * @param map
 * @return
 */
public static JSONObject toJSONObject(Map map) {
    Gson gson = new Gson();
    try {
        return new JSONObject(gson.toJson(map));
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

Gsoncom.google.gson.Gson谷歌的Gson

感悟:GsonUtil中还是有不少好用的工具类的,不错