Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JSON to Android Bundle [closed]

I want to convert a JSON string to android Bundle. The requirement was like to pass parameters to an activity directly from server as JSON rather than bundle. How to convert a JSON string to Android Bundle? Please provide abstract code if possible.

like image 572
Raj Avatar asked Mar 11 '15 10:03

Raj


People also ask

Can Android read JSON?

Android supports all the JSON classes such as JSONStringer, JSONObject, JSONArray, and all other forms to parse the JSON data and fetch the required information by the program. JSON's main advantage is that it is a language-independent, and the JSON object will contain data like a key/value pair.

How check JSONObject is null or not in android?

Try with json. isNull( "field-name" ) .

What is JSON exception in Android?

public class JSONException extends Exception. Thrown to indicate a problem with the JSON API. Such problems include: Attempts to parse or construct malformed documents. Use of null as a name.

What is JSON object and JSON array in Android?

Array: A JSONArray is enclosed in square brackets ([). It contains a set of objects. Object: Data enclosed in curly brackets ({) is a single JSONObject. Nested JSONObjects are possible and are very commonly used. Keys: Every JSONObject has a key string that's contains certain value.


1 Answers

This is late, but maybe it helps someone finding this thread:

/** 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;                                                                                                 
}      
like image 141
pscholl Avatar answered Oct 05 '22 15:10

pscholl