Android 앱 리소스에서 JSON 파일 사용 리소스 폴더에 JSON 콘텐츠가있는 파일이 있다고

내 앱의 원시 리소스 폴더에 JSON 콘텐츠가있는 파일이 있다고 가정합니다. JSON을 구문 분석 할 수 있도록 어떻게 이것을 앱으로 읽어 들일 수 있습니까?



답변

openRawResource를 참조하십시오 . 다음과 같이 작동합니다.

InputStream is = getResources().openRawResource(R.raw.json_file);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
    Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    int n;
    while ((n = reader.read(buffer)) != -1) {
        writer.write(buffer, 0, n);
    }
} finally {
    is.close();
}

String jsonString = writer.toString();

답변

Kotlin은 이제 Android의 공식 언어이므로 누군가에게 유용 할 것 같습니다.

val text = resources.openRawResource(R.raw.your_text_file)
                                 .bufferedReader().use { it.readText() }

답변

@kabuko의 답변을 사용 하여 리소스에서 Gson을 사용하여 JSON 파일에서로드하는 객체를 만들었습니다 .

package com.jingit.mobile.testsupport;

import java.io.*;

import android.content.res.Resources;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


/**
 * An object for reading from a JSON resource file and constructing an object from that resource file using Gson.
 */
public class JSONResourceReader {

    // === [ Private Data Members ] ============================================

    // Our JSON, in string form.
    private String jsonString;
    private static final String LOGTAG = JSONResourceReader.class.getSimpleName();

    // === [ Public API ] ======================================================

    /**
     * Read from a resources file and create a {@link JSONResourceReader} object that will allow the creation of other
     * objects from this resource.
     *
     * @param resources An application {@link Resources} object.
     * @param id The id for the resource to load, typically held in the raw/ folder.
     */
    public JSONResourceReader(Resources resources, int id) {
        InputStream resourceReader = resources.openRawResource(id);
        Writer writer = new StringWriter();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(resourceReader, "UTF-8"));
            String line = reader.readLine();
            while (line != null) {
                writer.write(line);
                line = reader.readLine();
            }
        } catch (Exception e) {
            Log.e(LOGTAG, "Unhandled exception while using JSONResourceReader", e);
        } finally {
            try {
                resourceReader.close();
            } catch (Exception e) {
                Log.e(LOGTAG, "Unhandled exception while using JSONResourceReader", e);
            }
        }

        jsonString = writer.toString();
    }

    /**
     * Build an object from the specified JSON resource using Gson.
     *
     * @param type The type of the object to build.
     *
     * @return An object of type T, with member fields populated using Gson.
     */
    public <T> T constructUsingGson(Class<T> type) {
        Gson gson = new GsonBuilder().create();
        return gson.fromJson(jsonString, type);
    }
}

이를 사용하려면 다음과 같이해야합니다 (예는에 있음 InstrumentationTestCase).

   @Override
    public void setUp() {
        // Load our JSON file.
        JSONResourceReader reader = new JSONResourceReader(getInstrumentation().getContext().getResources(), R.raw.jsonfile);
        MyJsonObject jsonObj = reader.constructUsingGson(MyJsonObject.class);
   }

답변

에서 http://developer.android.com/guide/topics/resources/providing-resources.html :

raw /
원시 형식으로 저장할 임의 파일. 원시 InputStream으로 이러한 리소스를 열려면 리소스 ID (R.raw.filename)를 사용하여 Resources.openRawResource ()를 호출합니다.

그러나 원래 파일 이름과 파일 계층에 액세스해야하는 경우에는 assets / 디렉토리에 일부 리소스를 저장하는 것이 좋습니다 (res / raw / 대신). assets /의 파일에는 리소스 ID가 제공되지 않으므로 AssetManager를 사용해서 만 읽을 수 있습니다.


답변

@mah 상태와 마찬가지로 Android 문서 ( https://developer.android.com/guide/topics/resources/providing-resources.html )에서는 json 파일이 / res (리소스) 아래의 / raw 디렉토리에 저장 될 수 있다고 말합니다. 프로젝트의 디렉토리, 예 :

MyProject/
  src/
    MyActivity.java
  res/
    drawable/
        graphic.png
    layout/
        main.xml
        info.xml
    mipmap/
        icon.png
    values/
        strings.xml
    raw/
        myjsonfile.json

내부 Activity, JSON 파일은 통해 액세스 할 수 있습니다 R(참고 자료) 클래스와 String으로 읽기 :

Context context = this;
Inputstream inputStream = context.getResources().openRawResource(R.raw.myjsonfile);
String jsonString = new Scanner(inputStream).useDelimiter("\\A").next();

이것은 Java 클래스를 사용하므로 Scanner간단한 텍스트 / json 파일을 읽는 다른 방법보다 코드 줄이 적습니다. 구분자 패턴 \A은 ‘입력의 시작’을 의미합니다. .next()이 경우 전체 파일 인 다음 토큰을 읽습니다.

결과 json 문자열을 구문 분석하는 방법에는 여러 가지가 있습니다.


답변

InputStream is = mContext.getResources().openRawResource(R.raw.json_regions);
                            int size = is.available();
                            byte[] buffer = new byte[size];
                            is.read(buffer);
                            is.close();
                           String json = new String(buffer, "UTF-8");

답변

사용 :

String json_string = readRawResource(R.raw.json)

기능 :

public String readRawResource(@RawRes int res) {
    return readStream(context.getResources().openRawResource(res));
}

private String readStream(InputStream is) {
    Scanner s = new Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}