your programing

Android에서 json 파일 읽기

lovepro 2020. 10. 11. 11:04
반응형

Android에서 json 파일 읽기


다음 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 프로젝트에서 생성 된 프로젝트의 경우 기본 폴더 아래에 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을 참조하십시오 .

원하는 것을 얻을 수 있기를 바랍니다.


이 시도:

JSONObject obj = new JSONObject(yourjsonstring);

참고 URL : https://stackoverflow.com/questions/13814503/reading-a-json-file-in-android

반응형