thumbPaths
및 captions
을 JSON 파일에서 가져 오려고합니다.JSON이 내 객체에 액세스 할 수 없습니다.
JSON 파일 : 나는 thumbPaths에서 얻는 이미지를 처리 할 수 있도록 자신의 배열에 저장하려고
"pictures": [
{
"title": "Animals",
"gallery":
[
{
"path":"",
"thumbPath":"",
"caption":""
},
{
"path":"",
"thumbPath":"",
"caption":""
},
{
"path":"",
"thumbPath":"",
"caption":""
},
{
"path":"",
"thumbPath":"",
"caption":""
},
{
"path":"",
"thumbPath":"",
"caption":""
},
{
"path":"",
"thumbPath":"",
"caption":""
},
{
"path":"",
"thumbPath":"",
"caption":""
},
{
"path":"",
"thumbPath":"",
"caption":""
},
{
"path":"",
"thumbPath":"",
"caption":""
}
]
},
{
"title": "Auroras",
"gallery":
[
{
"path":"",
"thumbPath":"",
"caption":""
}
]
},
{
"title": "Boats",
"gallery":
[
{
"path":"",
"thumbPath":"",
"caption":""
},
{
"path":"",
"thumbPath":"",
"caption":""
},
{
"path":"",
"thumbPath":"",
"caption":""
}
]
}
임. 은 내가 말하려고 위의 AsyncTask를
public class getJSONObjects extends AsyncTask<String, Void, String[]>{
@Override
protected String[] doInBackground(String... URL) {
// Access the JSONHandler for the URL
Log.d(tag, "in background on getJSON Artist Gallery.");
//initalize parser
JSONParser jparse = new JSONParser();
//call objects
JSONObject json = jparse.getJSONFromUrl(URL);
try {
pictures = new JSONArray(json.getString(TAG_PICTURES));
Log.d(tag, "after pictures");
} catch (JSONException e) {
e.printStackTrace();
}
// looping through All Pictures
for(int i = 0; i < pictures.length(); i++){
Log.d(tag, "in loop for pictures");
Log.d(tag, "Index:" + i);
JSONObject c;
try {
c = pictures.getJSONObject(i);
String title2 = c.getString(TAG_TITLE);
titles[i] = title2;
gallery = new JSONArray(c.getString(TAG_GALLERY));
Log.d(tag, "just got gallery array");
Log.d(tag, "before if statement");
Log.d(tag, "title:" + titles[i].toString());
if(title2 == titleTextView.getText().toString()){
Log.d(tag, "inside if statement");
for(int z = 0; z < gallery.length(); z++){
try {
JSONObject d = gallery.getJSONObject(z);
String thumbPath = d.getString(TAG_THUMBPATHS);
String captions = d.getString(TAG_CAPTIONS);
Log.d(tag, "thumbPath:" + thumbPath);
Log.d(tag, "captions:" + captions);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
이를 달성하기 위해 시도하고 그 제목은 만에 thumbPaths 및 캡션을 잡아해야한다는 JSON 배열의 제목 == 경우 그 제목 아래 갤러리. 그렇게하면 나는 오직 내가 필요한 것을 잡을 뿐이다.
JSON 파일에서 적절한 정보를 얻을 수 있습니까?
나는'title2.equals (titleTextView.getText(). toString())'이 아니라'title2 == ... '이 아니라고 생각합니다. – Ali
@Ali 당신의 권리, 고마워요! – Keeano
또한 필요한 경우 "잡아"싶으면 스트리밍 JSON API를 사용하여 조사해야한다고 생각합니다. 그렇게하면 큰 문자열을 메모리에 저장할 필요없이 객체를 만날 때 객체를 만들 수 있습니다 (일부 장치에서 충돌이 발생할 수 있음). 약간의 작업이지만 효율적인 메모리가 좋습니다. 나는 [Jackson] (http : //wiki.fasterxml.com/JacksonInFiveMinutes)에 대해 – Ali