2

I'm new to JSON android java eclipse. I am doing a listview with images and parsing json array. I followed this tutorial: http://www.wingnity.com/blog/android-json-parsing-and-image-loading-tutorial/ . In that tutorial, their JSON array contains the array name.However, mine doesn't contain the array name. So my question is how to code JSON Array without the array name?

Below is my JSON code.

[

 {  "event_id": "EV00000001",
    "event_title": "Movie 1",   
},
{
    "event_id": "EV00000002",
    "event_title": "Movie2",
    }
]

Below is my JSON coding for parsing the JSON.

  protected Boolean doInBackground(String... urls) {

    try { 
HttpGet httppost = new HttpGet(urls[0]);
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(httppost);

    // StatusLine stat = response.getStatusLine();
    int status = response.getStatusLine().getStatusCode();

    if (status == 200) {
        HttpEntity entity = response.getEntity();
        String data = EntityUtils.toString(entity);


        JSONObject jsono = new JSONObject(data);
        JSONArray jarray = jsono.getJSONArray("actors"); 

        for (int i = 0; i < jarray.length(); i++) {
            JSONObject object = jarray.getJSONObject(i);

            Events event = new Events();

            event.setevent_title(object.getString("event_title"));


            eventList.add(event);
        }
        return true;
    }

    //------------------>>

} catch (ParseException e1) {
    e1.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (JSONException e) {
    e.printStackTrace();
}
return false;
}

What am i suppose to do?

This problem is solved. Thus, i will be posting the correct code.

protected Boolean doInBackground(String... urls) {
            try {

                //------------------>>
                HttpGet httppost = new HttpGet(urls[0]);
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(httppost);

                // StatusLine stat = response.getStatusLine();
                int status = response.getStatusLine().getStatusCode();

                if (status == 200) {
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);

                JSONArray array = new JSONArray(data);

                for (int i = 0; i < array.length(); i++) {

                    JSONObject obj = array.getJSONObject(i);

                    Events event = new Events();

                    event.setevent_title(obj.getString("event_title"));

                    eventList.add(event);


                }
                return true;
            }

            //------------------>>

        } catch (ParseException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }
0

3 Answers 3

3

Here is a little example:

JSONArray array = new JSONArray();
    
JSONObject obj1 = new JSONObject();
obj1.put("key", "value");
array.put(obj1);
    
JSONObject obj2 = new JSONObject();
obj2.put("key2", "value2");
array.put(obj2);

That would look like:

[
    {
        "key": "value"
    },
    {
        "key2": "value2"
    }
]

If you want to get information about your JSONObjects in your JSONArray just iterate over them:

for (int i = 0; i < array.length(); i++) {
    JSONObject object = (JSONObject) array.get(i);
    object.get("event_id");
    object.get("event_title");
}
Sign up to request clarification or add additional context in comments.

2 Comments

i dont wanna input the value. i want it to retrieve the the value by giving key only.
So you should iterate over your json objects which are in your json array. I edit my answer.
1

You can use GSON librery, it's very easy to use. Just create a class (model)

public class Event{
  private String event_id;
  private String event_title;
  ...
}

and in your main activity

Event ev = new Event ("EV00000001", "Movie 1")
Gson gson = new Gson();
gSon = gson.toJson(ev); 
Log.i("gson", gSon);

and you will get JSON

[

{  "event_id": "EV00000001",
   "event_title": "Movie 1",   
}
]

Comments

1

What you have is an array of JSON Objects.

What the tutorial has is a JSON Object that has a property, which has an array of JSON objects.

When you look at these, here is a simple way to differentiate the two:

[] -> Array
{} -> Object

So in your code, instead of doing

JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("actors");  

You might want to do:

JSONArray jarray = new JSONArray(data); 

1 Comment

Sorry for the super late reply. I tried your method, but it crashed the app. End up a few weeks later, someone told me the same method as yours. i tried again and it works.

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.