查看此帖子Reading a json file in Android。如何用 -
替换下面的行json_return_by_the_function
我需要在字符串中保存函数返回值。
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("file_name.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;
}
JSONObject obj = new JSONObject(json_return_by_the_function);
答案 0 :(得分:0)
JSONObject obj = new JSONObject(loadJSONFromAsset());
答案 1 :(得分:0)
返回值是所有就绪字符串
String str1 = loadJSONFromAsset();
将其保存在您想要的地方,然后解析
JSONObject obj = new JSONObject(str1);