Java 在Android中使用本地json文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23808365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using local json file in Android
提问by user3658609
I will be using a local JSON file with some data. My question is where is the best/proper way to store the file and how should I access it and it's contents?
我将使用带有一些数据的本地 JSON 文件。我的问题是存储文件的最佳/正确方法在哪里,我应该如何访问它及其内容?
I have worked with json through http before but not a local one.
我以前通过 http 使用过 json,但不是本地的。
采纳答案by ranjk89
There are so many ways,
有很多方法,
You can store your JSON file in assets folder and read them like this - https://stackoverflow.com/a/19945484/713778
You can store it in res/raw folder and read the same as show here - https://stackoverflow.com/a/6349913/713778
您可以将 JSON 文件存储在资产文件夹中并像这样读取它们 - https://stackoverflow.com/a/19945484/713778
您可以将其存储在 res/raw 文件夹中并阅读与此处显示相同的内容 - https://stackoverflow.com/a/6349913/713778
For basic JSON parsing, Android's in-built JSONObject should work - https://developer.android.com/reference/org/json/JSONObject.html
对于基本的 JSON 解析,Android 的内置 JSONObject 应该可以工作 - https://developer.android.com/reference/org/json/JSONObject.html
For more advanced JSON parsing (json-java mapping), you can look at GSON library - https://code.google.com/p/google-gson/
对于更高级的 JSON 解析(json-java 映射),您可以查看 GSON 库 - https://code.google.com/p/google-gson/