JSON相关:JSON-bundle

JSON转成bundle

Intent intent = new Intent();
JSONObject extrasJson = new JSONObject(extras);
Iterator<String> keys=extrasJson.keys();
while (keys.hasNext()) {
    String key = keys.next();
    // 可以根据opt出来的不同类型put
    intent.put(key, extrasJson.optString(key));
}

这里有一个需要注意的地方,SDK19以后,用wrap可以更好的处理get出来是map、array等问题。

bundle转JSON 也就是遍历bundle

JSONObject json = new JSONObject();
Set<String> keys = bundle.keySet();
for (String key : keys) {
    try {
        // json.put(key, bundle.get(key)); see edit below
        json.put(key, JSONObject.wrap(bundle.get(key)));
    } catch(JSONException e) {
        //Handle exception here
    }
}

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注