Fabrizio Formichelli Fabrizio Formichelli - 1 year ago 56
Android Question

Android Studio - Parse JSON Keys

I am trying to parse a local json file. With the code I have so far I can read all the json however, I cannot return each individual key.

Json file (level.json)

"level1": {
"artist": "aaaa",
"title":"new title",
"level2": {
"artist": "bbb",
"title":"new title2",


What I am trying to do is to get for each level the key. For example I want to get "artist" from "level1".

public String loadJSONFromAsset() {
String json = null;
try {

InputStream is = getAssets().open("level.json");

int size = is.available();

byte[] buffer = new byte[size];



json = new String(buffer, "UTF-8");

} catch (IOException ex) {
return null;
return json;


When I print the function I get all the json.

JSONObject object = new JSONObject();

How can I get the keys depending of each json object?

Answer Source

You can use for instance:

JSONObject object = new JSONObject(your_json_string);
JSONObject level1 = object.getJSONObject("level1");
JSONObject level2 = object.getJSONObject("level2");

and now you can access artist, title and year like that: