2
我想解析本地的json文件。到目前为止,我可以读取所有json的代码,但不能返回每个单独的密钥。Android Studio - 解析JSON密钥
JSON文件(level.json)
{
"level1": {
"artist": "aaaa",
"title":"new title",
"year":"1990"
},
"level2": {
"artist": "bbb",
"title":"new title2",
"year":"1992"
}
}
什么我试图做的是让每个级别的关键。例如,我想从“level1”中获得“艺术家”。
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("level.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
当我打印的功能,我得到所有的JSON。
JSONObject object = new JSONObject();
text.setText(object.getString("level1"));
我该如何获得取决于每个json对象的键?
太谢谢你了....它的工作原理很good--我会在5分钟内接受你的回答 –