JSON not able to access my object
I am trying to get the thumbPaths and captions from my JSON file.
JSON FILE:
"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":""
}
]
}
Im trying to save them to their own arrays so that I can handle getting
images from the thumbPaths. I am attempting to achieve this in an
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();
}
In the above I am trying to say that if the title is == to the title in
the JSON array that it should only grab the thumbPaths and captions in the
gallery under that title. That way I only grab what I need and nothing
more.
How could I achieve getting the proper information from my JSON file?
No comments:
Post a Comment