반응형
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
반응형
'your programing' 카테고리의 다른 글
jQuery draggable이 페이지 스크롤 후 잘못된 위치에 도우미를 표시합니다. (0) | 2020.10.11 |
---|---|
Entity Framework를 사용하여 열의 최대 값을 얻는 방법은 무엇입니까? (0) | 2020.10.11 |
파이썬 스크립트를 실행하는 Windows 컴퓨터의 이름을 얻고 있습니까? (0) | 2020.10.11 |
둘 다에 대해 0-255 범위에서 RGB를 HSV로, HSV를 RGB로 변환하는 알고리즘 (0) | 2020.10.11 |
phpMyAdmin에서 데이터베이스 삭제 (0) | 2020.10.11 |