开发者

Iterating through multiple objects in JSON file

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
               开发者_JAVA技巧 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; 


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜