이것은 URL의 JSON 데이터입니다.
[
{ "title" : "65th Issue", "author": "అశోక్"},
{ "title" : "64th Issue", "author": "రాము" },
{ "title" : "63rd Issue", "author": "శ్రీను" }
]
JSONArray처럼 보이지만 액세스 할 이름 (배열 이름)이 없습니다. 누구든지 안드로이드 에서이 JSON 데이터를 어떻게 구문 분석 할 수 있는지 말해 줄 수 있습니까?
코드 구문 분석
InputStream inputStream = connection.getInputStream();
Reader reader = new InputStreamReader(inputStream);
int contentLength = connection.getContentLength();
char [] charArray = new char[contentLength];
reader.read(charArray);
String responseData = new String(charArray);
try{
JSONArray jArray = new JSONArray(responseData);
for(int i = 0; i < jArray.length(); i++) {
String title = jArray.getJSONObject(i).getString("title");
}
} catch (JSONException e) {
Log.v(TAG, "JSON EXCEPTION");
}
String jsonString = ...; //This contains the above mentioned String.
JSON 문자열의 경우 []는 배열을 나타내고 {}는 객체를 나타냅니다. 귀하의 경우 문자열은 배열을 의미하는 []로 시작하므로 먼저 JSONArray를 가져옵니다.
JSONArray jArray = new JSONArray(jsonString);
이제 배열에 {}로 시작하고 끝나는 여러 문자열이 있습니다. 즉, 배열에 여러 객체가 있음을 의미합니다. 따라서 배열 길이에 대해 루프를 실행하여 각 객체를 추출한 다음 객체에서 키 값을 추출합니다.
for(int i = 0; i < jArray.length(); i++) {
String title = jArray.getJSONObject(i).getString("title");
}
따라서 완전한 코드는 다음과 같습니다.
String jsonString = ...; //This contains the above mentioned String.
JSONArray jArray = new JSONArray(jsonString);
for(int i = 0; i < jArray.length(); i++) {
String title = jArray.getJSONObject(i).getString("title");
}
편집 2 :
String jsonString = "[\r\n { \"title\" : \"65th Issue\", \"author\": \"\u0C05\u0C36\u0C4B\u0C15\u0C4D\"},\r\n { \"title\" : \"64th Issue\", \"author\": \"\u0C30\u0C3E\u0C2E\u0C41\" },\r\n { \"title\" : \"63rd Issue\", \"author\": \"\u0C36\u0C4D\u0C30\u0C40\u0C28\u0C41\" }\r\n]";
try {
JSONArray jArray = new JSONArray(jsonString);
for (int i = 0; i < jArray.length(); i++) {
String title = jArray.getJSONObject(i).getString("title");
Log.d(LOGTAG, title);
}
} catch (JSONException e) {
e.printStackTrace();
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다