阅读在Android中的JSON文件 [英] Reading a json file in Android
问题描述
我有以下的JSON文件。我想知道我应该把JSON文件在我的项目以及如何读取并存储。
{
aakash:[
[0.070020,0.400684]
[0.134198,0.515837]
[0.393489,0.731809]
[0.281616,0.739490]
]
anuj:[
[1287.836667,-22.104523]
[-22.104523,308.689613]
[775.712801,-13.047385]
[-13.047385,200.067743]
]
}
放入资产文件。
对于Android的Studio项目需要主文件夹下创建的资产文件夹中创建项目。
阅读文件
公共字符串loadJSONFromAsset(){
JSON字符串= NULL;
尝试 {
InputStream的是= getAssets()开(file_name.json)。
INT大小= is.available();
byte []的缓冲区=新的字节[尺寸]
is.read(缓冲液);
is.close();
JSON =新的String(缓冲区,UTF-8);
}赶上(IOException异常前){
ex.printStackTrace();
返回null;
}
返回JSON;
}
然后你可以简单地阅读字符串
这个函数返回为
的JSONObject的obj =新的JSONObject(json_return_by_the_function);
有关详细信息,关于JSON见 <一href="http://www.vogella.com/articles/AndroidJSON/article.html">http://www.vogella.com/articles/AndroidJSON/article.html
希望U将得到什么ü希望
I have the following json file. I want to know where should i place the json file in my project and how to read and store it.
{
"aakash": [
[ 0.070020,0.400684],
[ 0.134198,0.515837],
[ 0.393489,0.731809],
[ 0.281616,0.739490]
],
"anuj": [
[ 1287.836667,-22.104523],
[ -22.104523,308.689613],
[ 775.712801,-13.047385],
[ -13.047385,200.067743]
]
}
Put that file in assets.
For project created in Android Studio project you need to create assets folder under the main folder.
Read that file as
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;
}
and then you can simply read this string
return by this function as
JSONObject obj = new JSONObject(json_return_by_the_function);
For further details regarding JSON see http://www.vogella.com/articles/AndroidJSON/article.html
Hope u will get what u want
这篇关于阅读在Android中的JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!