首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将Bundle转换为JSON

将Bundle转换为JSON
EN

Stack Overflow用户
提问于 2014-02-18 23:42:30
回答 4查看 26.6K关注 0票数 28

我想将an Intent的extras Bundle转换为JSONObject,这样我就可以在JavaScript之间传递它。

有没有一种快速或最好的方法来完成这个转换?如果不是所有可能的捆绑包都能工作,那就没问题了。

EN

回答 4

Stack Overflow用户

发布于 2016-06-09 22:04:55

代码语言:javascript
复制
private String getJson(final Bundle bundle) {
    if (bundle == null) return null;
    JSONObject jsonObject = new JSONObject();

    for (String key : bundle.keySet()) {
        Object obj = bundle.get(key);
        try {
            jsonObject.put(key, wrap(bundle.get(key)));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return jsonObject.toString();
}

public static Object wrap(Object o) {
    if (o == null) {
        return JSONObject.NULL;
    }
    if (o instanceof JSONArray || o instanceof JSONObject) {
        return o;
    }
    if (o.equals(JSONObject.NULL)) {
        return o;
    }
    try {
        if (o instanceof Collection) {
            return new JSONArray((Collection) o);
        } else if (o.getClass().isArray()) {
            return toJSONArray(o);
        }
        if (o instanceof Map) {
            return new JSONObject((Map) o);
        }
        if (o instanceof Boolean ||
                o instanceof Byte ||
                o instanceof Character ||
                o instanceof Double ||
                o instanceof Float ||
                o instanceof Integer ||
                o instanceof Long ||
                o instanceof Short ||
                o instanceof String) {
            return o;
        }
        if (o.getClass().getPackage().getName().startsWith("java.")) {
            return o.toString();
        }
    } catch (Exception ignored) {
    }
    return null;
}

public static JSONArray toJSONArray(Object array) throws JSONException {
    JSONArray result = new JSONArray();
    if (!array.getClass().isArray()) {
        throw new JSONException("Not a primitive array: " + array.getClass());
    }
    final int length = Array.getLength(array);
    for (int i = 0; i < length; ++i) {
        result.put(wrap(Array.get(array, i)));
    }
    return result;
}
票数 6
EN

Stack Overflow用户

发布于 2017-07-31 23:39:44

票数 3
EN

Stack Overflow用户

发布于 2021-09-30 13:10:02

如果包有嵌套的包,那么JSONObject.wrap(bundle.get(key))将返回null。所以我设法用这个递归函数让它在我的用例中工作。不过,还没有测试更高级的用例。

代码语言:javascript
复制
JSONObject json = convertBundleToJson(bundle);

public JSONObject convertBundleToJson(Bundle bundle) {
    JSONObject json = new JSONObject();
    Set<String> keys = bundle.keySet();

    for (String key : keys) {
        try {
            if (bundle.get(key) != null && bundle.get(key).getClass().getName().equals("android.os.Bundle")) {
                Bundle nestedBundle = (Bundle) bundle.get(key);
                json.put(key, convertToJson(nestedBundle));
            } else {
                json.put(key, JSONObject.wrap(bundle.get(key)));
            }
        } catch(JSONException e) {
            System.out.println(e.toString());
        }
    }

    return json;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21858528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档