'Getting an inner bundle from a bigger bundle in Android

I have a function in my Android code where I get a bundle as input:

void f(Bundle data)

This data is actually in the form of a json. Suppose it is in the following format: {"a":"x", "b":"y", "content":{"a1":"x1", "b1":"y1"}}

In such cases, if I want to get the value of a, or b, then I would need to do String a = data.getString("a"); which would fetch me the string "x". Similarly, String content = data.getString("content") would return me the string {"a1":"x1", "b1":"y1"}}. But I cannot figure out how to get the specific values inside content itself. Is there any way by which I can get content as another bundle just like data so that I can get the values inside it by doing content.getString("a1") or something like that. Is that possible?



Solution 1:[1]

JSONObject jOBj = new JSONobject(data.getString("content"));
String a1 = jOBj.getString("x1");
String b1 = jOBj.getString("y1");

try this

Solution 2:[2]

To get value from bundle and create JSon object/ Hashmap

private void createFlatJSon(Bundle appRestrictions,JSONObject jsonObject) throws JSONException{
    for (String key : appRestrictions.keySet()) {
        if (appRestrictions.get(key) instanceof Bundle) {
            createFlatJSon((Bundle) appRestrictions.get(key),jsonObject);
        }else if (appRestrictions.get(key) instanceof Parcelable[]){
            for (int i=0;i< ((Parcelable[]) appRestrictions.get(key)).length; i++){
                createFlatJSon((Bundle)((Parcelable[]) appRestrictions.get(key))[i],jsonObject);
            }
            //Log.e("KEY skipped",appRestrictions.get(key).toString());
        }else{

           Log.e("KEY: ",key+" Value:"+appRestrictions.get(key).toString());
            jsonObject.put(key,appRestrictions.get(key).toString());

        }
    }


}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2