我正在尝试从我的 JSON 文件中获取thumbPaths
and 。captions
JSON文件:
"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":""
}
]
}
我试图将它们保存到自己的数组中,以便我可以处理从 thumbPaths 获取图像。我试图在 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();
}
在上面我想说的是,如果标题是 == 到 JSON 数组中的标题,它应该只抓取该标题下画廊中的 thumbPaths 和标题。这样我只抓住我需要的东西,仅此而已。
我怎样才能从我的 JSON 文件中获取正确的信息?