1

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).

4
  • this json string is invalid. Commented Mar 20, 2013 at 17:01
  • @KunalK I know that. I have simplified it. Assume it works. Let me edit the question and mention that. Commented Mar 20, 2013 at 17:03
  • Would there be any way for you to change the server response? IMO, it would be best to change it to always return an array, so if there is one image, it would just contain an array of size 1. Commented Mar 20, 2013 at 17:30
  • @BrentHronik. I am afraid that is not possible at the time being. Because the back end is automated and I do not have control on it. Commented Mar 20, 2013 at 18:41

3 Answers 3

1

I think you should use google gson jsonreader or android jsonreader that avaible from android API-11 the JSONreader read per token and you can use the function peek() to see the JSONToken type without consuming it in your case it'll be like this

main function

//response from the server
    response = myClient.execute(myConnection);
    Reader streamReader = new InputStreamReader(response
                            .getEntity().getContent());
    JsonReader reader = new JsonReader(streamReader);

reader.beginObject();

    while (reader.hasNext()) {

        String name = reader.nextName();

        if (name.equals("reports")) {
            readReports(reader);
        } else {
            reader.skipValue(); // avoid some unhandle events
        }
    }

reader.endObject();
reader.close();

readReports function

private void readReports(JsonReader reader) throws IOException {
    reader.beginArray();
    while(reader.hasNext()) {
    reader.beginObject();
    while (reader.hasNext()) {
        String objectNewsName = reader.nextName();
        if (objectNewsName .equals("News")) {
             readNews(reader);
        } else {
             reader.skipValue();
        }
    reader.endObject();

    }
    reader.endObject();
}

readNews function

private void readNews(JsonReader reader) throws IOException {
    reader.beginObject();
    while(reader.hasNext()) {
        String objectNewsDataName = reader.nextName();
        if (objectNewsDataName .equals("Title")) {
             Log.d("NEWS",reader.nextString());
        } else if (objectNewsDataName .equals("Info")) {
             Log.d("NEWS",reader.nextString());
        } else if (objectNewsDataName .equals("field_image")) {
             if(reader.peek() == JsonToken.BEGIN_ARRAY) {
                 readFieldImage(reader);
             } else {
                 Log.d("NEWS",reader.nextString());
             }
        }else {
             reader.skipValue();
        }
    }
    reader.endObject();
}

readFieldImage function

private void readFieldImage (JsonReader reader) throws IOException {
        reader.beginArray();
        while(reader.hasNext()) {
           Log.d("NEWS",reader.nextString());  //you will get the field image array content here
        }
    }

I hope my answer is clear enough but if you have some question about my answer feel free to ask in the comment :)

Sign up to request clarification or add additional context in comments.

2 Comments

I am afraid large portion of people downloading the app will be using API 8-10 ... so I am afraid this is not the best approach.
@tony9099 for API8-10 you can download google gson -> code.google.com/p/google-gson it's have the same jsonreader as android have in API-11 and you can use it in android API 8-10 I already used it before :)
0

have you tried this:

....
String title = node.getString("Title");
String info = node.getString("Info");
JSONArray fieldImageArray = node.getJSONArray("fieldimage");
//iterate fieldImageArray here
....

Comments

0

try like this

List<String> url= new ArrayList<String>();

     JSONArray field_image= news.getJSONArray("field_image");
    for (int i = 0; i < field_image.length(); i++) {
        url.add(field_image.get(i).toString());//adding to List
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.