将JSON转换为Android Bundle

11

我想将一个 JSON 字符串转换成 Android Bundle。要求直接从服务器以JSON格式传递参数到活动中,而不是使用Bundle。如何将 JSON 字符串转换为 Android Bundle?如果可能,请提供抽象代码。

4个回答

17
public static Bundle jsonStringToBundle(String jsonString){
    try {
        JSONObject jsonObject = toJsonObject(jsonString);
        return jsonToBundle(jsonObject);
    } catch (JSONException ignored) {

    }
    return null;
}
public static JSONObject toJsonObject(String jsonString) throws JSONException {
    return new JSONObject(jsonString);
}
public static Bundle jsonToBundle(JSONObject jsonObject) throws JSONException {
    Bundle bundle = new Bundle();
    Iterator iter = jsonObject.keys();
    while(iter.hasNext()){
        String key = (String)iter.next();
        String value = jsonObject.getString(key);
        bundle.putString(key,value);
    }
    return bundle;
}

问题在于它无法处理嵌套对象。 - kassim

13

虽然已经有些晚了,但或许对于正在寻找此线程的某些人还是有帮助的:

/** Convert a JSON object to a Bundle that can be passed as the extras of                                          
 * an Intent. It passes each number as a double, and everything else as a                                          
 * String, arrays of those two are also supported. */                                                              
public static Bundle fromJson(JSONObject s) {                                                                      
    Bundle bundle = new Bundle();                                                                                  

    for (Iterator<String> it = s.keys(); it.hasNext(); ) {                                                         
        String key = it.next();                                                                                    
        JSONArray arr = s.optJSONArray(key);                                                                       
        Double num = s.optDouble(key);                                                                             
        String str = s.optString(key);                                                                             

        if (arr != null && arr.length() <= 0)                                                                      
            bundle.putStringArray(key, new String[]{});                                                            

        else if (arr != null && !Double.isNaN(arr.optDouble(0))) {                                                 
            double[] newarr = new double[arr.length()];                                                            
            for (int i=0; i<arr.length(); i++)                                                                     
                newarr[i] = arr.optDouble(i);                                                                      
            bundle.putDoubleArray(key, newarr);                                                                    
        }                                                                                                          

        else if (arr != null && arr.optString(0) != null) {                                                        
            String[] newarr = new String[arr.length()];                                                            
            for (int i=0; i<arr.length(); i++)                                                                     
                newarr[i] = arr.optString(i);                                                                      
            bundle.putStringArray(key, newarr);                                                                    
        }                                                                                                          

        else if (!num.isNaN())                                                                                     
            bundle.putDouble(key, num);                                                                            

        else if (str != null)                                                                                      
            bundle.putString(key, str);                                                                            

        else                                                                                                       
            System.err.println("unable to transform json to bundle " + key);                                       
    }                                                                                                              

    return bundle;                                                                                                 
}      

-1
jaffa的回答很好,但它只适用于深度为1的JSON对象。我通过添加对嵌套对象的支持进行了改进。
private static Bundle jsonStringToBundle(String jsonString) {
    try {
        JSONObject jsonObject = new JSONObject(jsonString);
        return jsonToBundle(jsonObject);
    } catch (JSONException ignored) {}

    return null;
}

private static Bundle jsonToBundle(JSONObject jsonObject) throws JSONException {
    Bundle bundle = new Bundle();
    Iterator iter = jsonObject.keys();

    while (iter.hasNext()) {
        String key = (String)iter.next();
        String value = jsonObject.getString(key);
        Bundle bundleVal = jsonStringToBundle(value);

        if (bundleVal != null) {
            bundle.putBundle(key, bundleVal);
        } else {
            bundle.putString(key, value);
        }
    }

    return bundle;
}

使用方法:

Bundle bundle = jsonStringToBundle("{...}");

-11

快速 SSCCEE

A.class

// key for bundle ...
public static final JSON_STRING = "jsonString";

Intent intent = new Intent(A.this, B.class);
Bundle bundle = new Bundle();
bundle.putString(JSON_STRING,json.toString());
intent.putExtras(bundle);
startActivity(intent);

然后在 B.class 中...

Intent intent = getIntent();
Bundle extras = intent.getExtras();
String jsonString = extras.getString(A.JSON_STRING);

了解有关JSON和Java的更多信息


我是指将JSON对象转换为Bundle对象。 - Raj
这并没有回答问题。 - Matthew

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接