android studio로 json파일 불러오기
조회수 7167회
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
네. 가능합니다. json파일을 assets 폴더안에 넣어서 getAssets().open()메소드를 이용해 json 파일을 읽어와 string으로 변환하여 JSONObject로 변환해주면 됩니다.
간단하게 아래와 같은 코드가 되겠네요.
public String loadJSONFromAsset() { String json = null; try { InputStream is = getAssets().open("파일명.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; } JSONObject obj = new JSONObject(json_return_by_the_function); //JSONObject를 파싱하여 원하는 대로 리스트에 그려주면 됩니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력