0

I'm parsing a JSON file that I'm retrieving by accessing an API. Right now, I'm able to create an ArrayList of objects of my Offer class, but I'm only reading the first JSON object and grabbing the strings I'm interested in. How do I go about creating as many of my own Offer objects as there are in the JSON file?

In other words, I need to iterate through the JSON file and grab all of the offers.

The JSON looks like this:

{"offer":"expiration":"2011-04-08T02:30:00Z","valid_from":"2011-04-07T12:00:31Z","business":{"address":{"state":"NY","zip":"10013","cross_streets":"Chatham Sq & Worth St","address_1":"12 Mott St","address_2":null,"city":"New York"},"phone":"2126192989","published":"2011-04-07T12:00:33Z","rescinded_at":null,"valid_to":"2011-04-08T02:00:00Z"}}, {"offer":"expiration":"2011-04-08T02:30:00Z","valid_from":"2011-04-07T12:00:31Z","business":{"address":{"state":"NY","zip":"10013","cross_streets":"Chatham Sq & Worth St","address_1":"12 Mott St","address_2":null,"city":"New York"},"phone":"2126192989","published":"2011-04-07T12:00:33Z","rescinded_at":null,"valid_to":"2011-04-08T02:00:00Z"}}, {"offer":"expiration":"2011-04-08T02:30:00Z","valid_from":"2011-04-07T12:00:31Z","business":{"address":{"state":"NY","zip":"10013","cross_streets":"Chatham Sq & Worth St","address_1":"12 Mott St","address_2":null,"city":"New York"},"phone":"2126192989","published":"2011-04-07T12:00:33Z","rescinded_at":null,"valid_to":"2011-04-08T02:00:00Z"}}

As you can see, there is one offer object after another...

Here's my code so far:

        ArrayList<Offer> offerList = new ArrayList<Offer>();

        for(String url: urls) {
            OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
            consumer.setTokenWithSecret("", "");

            try {

                URL url1 = new URL(url);
                HttpURLConnection request = (HttpURLConnection) url1.openConnection();

                // sign the request
                consumer.sign(request);

                // send the request
                request.connect();


                String JSONString = convertStreamToString(request.getInputStream());

                JSONObject jObject = new JSONObject(JSONString);

                JSONObject offerObject = jObject.getJSONObject("offer");

                String titleValue = offerObject.getString("title");
                //System.out.println(titleValue);

                String descriptionValue = offerObject.getString("description");
                //System.out.println(attributeValue);
                JSONObject businessObject = offerObject.getJSONObject("business");
                String nameValue = businessObject.getString("name");

                Offer myOffer = new Offer(titleValue, descriptionValue, nameValue);

                offerList.add(myOffer);
                Log.v("ArrayList:", offerList.toString());

            } catch (Exception e) {
                e.printStackTrace();

            } 
        }
        return offerList; 

1 Answer 1

2

The JSON you have presented is not valid JSON.

If you put a '[' at the beginning and a ']' at the end, it becomes a valid JSONArray.

JSONArray JavaDoc

You should be able to do something like this:

JSONArray array = new JSONArray(inputJSON);
for(int index = 0; index < array.length(); ++index) {
  JSONObject offerObject = array.getJSONObject(index);
  //... your offer calculation...add offer to list...
}

If you have a JSONArray of JSONObjects of your Offer JSON (which you would if you add the brackets like I suggested), then you can iterate over the length of the JSONArray, get your JSONObject on each pass, and create the Offer just as you did in the example you provided.

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

1 Comment

Crap, you're right. I used substring to cut off the "[" and "]" at the beginning and end of the file because I thought my convertStreamToString method was adding that, but it's not. So I have JSONArray. Can you show me how to iterate through the Array? I'm having trouble...

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.