Search code examples
androidbundle

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

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

    try this