Java 如何将json文件添加到android项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40565078/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:48:09  来源:igfitidea点击:

how to add json file to android project

javaandroidjson

提问by Jeff Louzada

I am simply trying to add a .json file into my android studio project. Most of what I have run into has you create a folder and add it there but I want to simply move it over under app. Here Is my project image

我只是想将 .json 文件添加到我的 android studio 项目中。我遇到的大部分内容都是您创建一个文件夹并将其添加到那里,但我想简单地将它移到应用程序下。这是我的项目图片

How to add JSON files to my project

如何将 JSON 文件添加到我的项目中

Add JSON file to app

将 JSON 文件添加到应用程序

采纳答案by CommonsWare

I want to simply move it over under app

我想简单地将它移到应用程序下

If you literally mean that you want to have app/something.json, you are welcome to put the file there, but it will not be packaged with your app, and it will not be available to you at runtime.

如果您的字面意思是想要拥有app/something.json,欢迎您将文件放在那里,但它不会与您的应用程序打包在一起,并且在运行时您将无法使用它。

If you want to ship the JSON with your app, you have four major options:

如果您想将 JSON 与您的应用程序一起发送,您有四个主要选项:

  • Put it in assets/and read it in using AssetManagerand its open()method to get an InputStream

  • Put it in res/raw/and read it in using Resourcesand its openRawResource()method

  • Hardcode it as a string in Java code

  • Write yourself a code generator that converts JSON into a Java class that you would access like you do the code-generated Rand BuildConfigclasses

  • 把它放进assets/去读它在使用AssetManager和它的open()方法得到一个InputStream

  • 把它放进res/raw/去读它在使用Resources和它的openRawResource()方法

  • 将其硬编码为 Java 代码中的字符串

  • 自己写一个代码生成器,转换成JSON Java类,你会获得像你这样的代码生成RBuildConfig

It is possible that such a code generator already exists. I have a rudimentary Gradle pluginthat does this, as an example that I'll be including in the next update of my book.

可能已经存在这样的代码生成器。我有一个基本的 Gradle 插件可以做到这一点,作为一个例子,我将包含在我的书的下一次更新中。

回答by Morozov

Put it androidTest/assets/ Just create file, like a SomeFile.json

把它 androidTest/assets/ 就创建file,就像一个SomeFile.json

回答by Viral Patel

Please follow the steps below and json file in our project.

请按照以下步骤和我们项目中的 json 文件进行操作。

step 1 : Change Android to Project

第 1 步:将 Android 更改为 Project

Step 2 : create assets Folder and put json file like login.json

第 2 步:创建资产文件夹并放置 json 文件,如 login.json

enter image description here

在此处输入图片说明

I hope it will help you...!

我希望它会帮助你...!