Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4627386
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:33:37+00:00 2026-05-22T03:33:37+00:00

I’m parsing a JSON file that I’m retrieving by accessing an API. Right now,

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:33:38+00:00Added an answer on May 22, 2026 at 3:33 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.