将JSON转换为Android Bundle

Raj*_*Raj 7 java android json bundle adt

我想将一个JSON字符串转换为android Bundle.要求就是直接从服务器将参数作为JSON而不是bundle传递给活动.如何将JSON字符串转换为Android Bundle?如果可能,请提供抽象代码.

Nic*_*ick 14

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;
}
Run Code Online (Sandbox Code Playgroud)


psc*_*oll 12

这已经晚了,但也许它可以帮助找到这个帖子的人:

/** 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;                                                                                                 
}      
Run Code Online (Sandbox Code Playgroud)


Mat*_*lár -11

只是一个快速的SSCCEE

一类

// 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);
Run Code Online (Sandbox Code Playgroud)

然后在B班...

Intent intent = getIntent();
Bundle extras = intent.getExtras();
String jsonString = extras.getString(A.JSON_STRING);
Run Code Online (Sandbox Code Playgroud)

有关 json 和 java 的更多信息