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 9266251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:16:30+00:00 2026-06-18T14:16:30+00:00

I have a json file formatted like this (only the actual file has no

  • 0

I have a json file formatted like this (only the actual file has no whitespace):

{
    "main": [
        {
            "sections": [
                "sec1",
                "sec2"
            ],
            "title": "Section List 1"
        },
        {
            "sections": [
                "sec3",
                "sec4",
                "sec5"
            ],
            "title": "Section List 2"
        }
    ],
    "sections": {
        "sec1": {
            "products": [
                "prod1",
                "prod2"
            ]
        },
        "sec2": {
            "products": [
                "prod3"
            ]
        }
    },
    "products": {
        "prod1": {
            "url": "url1.gif",
            "title": "Product 1"
        },
        "prod2": {
            "url": "url2.gif",
            "title": "Product 2"
        },
        "prod3": {
            "url": "url3.gif",
            "title": "Product 3"
        }
    }
}

When I attempt to send the data for that file into JSONObject, the JSONObject being loaded only contains the final object in the top list, in this case “products”. Right now, my code to load the JSONObject is this:

JSONObject dlResult = new JSONObject(new Scanner(cnxn.getInputStream()).nextLine());

I’ve also tried storing the data in a String first and giving that to the JSONObject constructor, I’ve tried giving that String to a JSONTokener first, and giving that tokener to the JSONObject constructor. Both the String and the JSONTokener contain the entire file, but once it gets put into the JSONObject, it’s always the same thing – “main” and “sections” get cut out, and only “products” remains.

Here’s the rest of my relevant code:

public class MapsListFragment extends ListFragment
{
    private static JSONObject mDlResult;
    private ArrayAdapter<MapInfo> mMapListAdapter = null;

    private class MapInfo
    {
        private String mText;
        private String mURL;

        MapInfo(String inText, String inURL)
        {
            mText = inText;
            mURL = inURL;
        }

        public String URL()
        {
            return mURL;
        }

        @Override
        public String toString()
        {
            return mText;
        }
    }

public class MapsListUpdater extends AsyncTask<String, String, JSONObject>
    {
        private static final String URL = "http://jsonURL/products.json";

        private Date lastUpdate;

        @Override
        protected JSONObject doInBackground(String... inObjects)
        {
            Date ifModifiedSince = lastUpdate;

            try 
            {
                HttpURLConnection cnxn = (HttpURLConnection) new URL(URL).openConnection();
                if (ifModifiedSince != null)
                    cnxn.setIfModifiedSince(ifModifiedSince.getTime());

                cnxn.connect();
                if (cnxn.getResponseCode() != HttpURLConnection.HTTP_NOT_MODIFIED)
                {
                    mDlResult = new JSONObject(new Scanner(cnxn.getInputStream()).nextLine());
                }

                cnxn.disconnect();
            } 
            catch (Exception e) 
            {
                boolean check = true;
            }

            return mDlResult;
        }

        @Override
        protected void onPostExecute(JSONObject result)
        {
            super.onPostExecute(result);
            try 
            {
                // This is really ugly and brute-forcey, but it's the only way I could get JSONObjects populated with ALL the data!
                /*String[] tryThis = mDlResult.split(",\"sections\":");
                tryThis[0] += '}';
                tryThis[1] = tryThis[1].substring(0, tryThis[1].indexOf("]}},\"products\":"));
                tryThis[1] = "{\"sections\":" + tryThis[1] + "]}}}";*/

                JSONObject mainObj = mDlResult.getJSONObject("main");//new JSONObject(mDlResult);

                JSONArray mainArray = mainObj.getJSONArray("main");

                Vector<String> titles = new Vector<String>();

                mMapListAdapter.clear();

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

                    String title = object.getString("title");

                    if(!titles.contains(title))
                    {
                        titles.add(title);
                        mMapListAdapter.add(new MapInfo(object.getString("title"), null));
                    }
                }
            }
            catch (Exception e) 
            {
                boolean check = true;
            } 
        }
    }
}
  • 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-06-18T14:16:31+00:00Added an answer on June 18, 2026 at 2:16 pm

    I feel a little foolish now. As it turns out, the JSONObject was getting EXACTLY what I asked it for… only the objects within it were getting reordered, so I couldn’t see the first 2 objects in my debugger!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a json file exported from phpmyadmin, it looks like this(utf-8 file): [{user_email:
I have a text file that is formatted like JSON, but in a print/view
I have this .json file: [ { id: 1, title: Ben\\'s First Blog Post,
I have a json file that looks like this: { status: 200, data: {
I have a json file with this structure: myCallback({ title: data, content: data, images:
I have a JSON file which contains a pound symbol in it. This json
i have a json file with many elements like these: { code : hfuiew89,
I have a json file of data and the only item for time i
I have generated a JSON file and it is formatted correctly, the same way
I have this json file. Part of it is given below : { has_more

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.