代码之家  ›  专栏  ›  技术社区  ›  Raj

将JSON转换为Android捆绑包[已关闭]

  •  11
  • Raj  · 技术社区  · 9 年前

    我想将JSON字符串转换为android Bundle。该要求类似于将参数作为JSON而不是捆绑包直接从服务器传递给活动。如何将JSON字符串转换为Android捆绑包?如果可能,请提供抽象代码。

    4 回复  |  直到 9 年前
        1
  •  17
  •   Nick    9 年前
    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;
    }
    
        2
  •  13
  •   pscholl    8 年前

    这很晚了,但也许它有助于某人找到这条线索:

    /** 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;                                                                                                 
    }      
    
        3
  •  -1
  •   jcady    6 年前

    jaffa的答案很好,但它只适用于depth=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("{...}");
    
        4
  •  -11
  •   Matej Å pilár    9 年前

    快一点 SSCCEE

    A、 类

    // 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、 类 ...

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

    more info about json and java