I am retrieving JSON from my website and trying to parse it in my android app. This is an automatic JSON generated by the back-end.
Whenever there is only single image uploaded, I am getting the link of the image without an array. That is as a single string.
However, in case there were images more than one uploaded I am getting them inside an array.(see JSON below) (JSON is invalid, as I removed some stuff that I do not use)
{
"Reports": [
{
"News": {
"Title": "Big Explosion",
"Info": "Lorem ipsum news etc here get here etc",
"field_image": "http://mysite.com/1.jpg"
}
},
{
"News": {
"Title": "2nd explosion",
"Info": "<p>Us a delimited list ws etc here get her</p>\n",
"field_image": [
"http://mysite.com/2.jpg",
"http://mysite.com/3.jpg",
"http://mysite.com/4.jpg"
]
}
]
}
I am using the below code in order to retrieve the JSON. However, I am not able to get each String alone when there is more than a single image.
JSONObject json = jParser.getJSONFromUrl(url);
if (json != null)
{
JSONArray ReportsJsonArray = json.getJSONArray("Reports");
for (int i = 0; i < ReportsJsonArray.length(); i++)
{
JSONObject c = ReportsJsonArray .getJSONObject(i);
JSONObject news= node.getJSONObject("News");
String title = node.getString("Title");
String info = node.getString("Info");
String fieldImage = node.getString("fieldimage");
if (fieldImage.charAt(0) == '[')
{
Log.i("tag", "more than 1 image");
// HOW TO GET THEM
} else
{
Log.i("tag", "single image");
//already have them
}
}
My code works but gets the whole array as a single string. (I have omitted the try-catch blocks to make the code simpler).