gpt4 book ai didi

android - 在 Android 中读取 json 文件

转载 作者:IT老高 更新时间:2023-10-28 12:43:07 32 4
gpt4 key购买 nike

我有以下 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]
]
}

最佳答案

将该文件放入 Assets 中

对于在 Android Studio 项目中创建的项目,您需要在主文件夹下创建 assets 文件夹。

将该文件读取为:

public String loadJSONFromAsset(Context context) {
String json = null;
try {
InputStream is = context.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;

}

然后你可以简单地读取这个函数返回的 string

JSONObject obj = new JSONObject(json_return_by_the_function);

有关 JSON 的更多详细信息,请参阅 http://www.vogella.com/articles/AndroidJSON/article.html

希望你能得到你想要的。

关于android - 在 Android 中读取 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13814503/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com