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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:58:36+00:00 2026-05-23T18:58:36+00:00

I have a JSON file with some arrays in it. I want to iterate

  • 0

I have a JSON file with some arrays in it. I want to iterate through the file arrays and get their elements and their values.

This is how my file looks like:

{
"JObjects": {
    "JArray1": [
        {
            "A": "a",
            "B": "b",
            "C": "c"
        },
        {
            "A": "a1",
            "B": "b2",
            "C": "c3",
            "D": "d4"
            "E": "e5"
        },
        {
            "A": "aa",
            "B": "bb",
            "C": "cc",
            "D": "dd"
        }

    ]
}

}       

This is how far I have come:

JSONObject object = new JSONObject("json-file.json");
JSONObject getObject = object.getJSONObject("JObjects");
JSONArray getArray = getObject.getJSONArray("JArray1");

for(int i = 0; i < getArray.length(); i++)
{
      JSONObject objects = getArray.getJSONArray(i);
      //Iterate through the elements of the array i.
      //Get thier value.
      //Get the value for the first element and the value for the last element.
}

Is it possible to do something like this?

The reason I want to do it like this is because the arrays in the file have a different number of elements.

  • 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-23T18:58:37+00:00Added an answer on May 23, 2026 at 6:58 pm

    Change

    JSONObject objects = getArray.getJSONArray(i);
    

    to

    JSONObject objects = getArray.getJSONObject(i);
    

    or to

    JSONObject objects = getArray.optJSONObject(i);
    

    depending on which JSON-to/from-Java library you’re using. (It looks like getJSONObject will work for you.)

    Then, to access the string elements in the “objects” JSONObject, get them out by element name.

    String a = objects.get("A");
    

    If you need the names of the elements in the JSONObject, you can use the static utility method JSONObject.getNames(JSONObject) to do so.

    String[] elementNames = JSONObject.getNames(objects);
    

    “Get the value for the first element and the value for the last element.”

    If “element” is referring to the component in the array, note that the first component is at index 0, and the last component is at index getArray.length() - 1.


    I want to iterate though the objects in the array and get thier component and thier value. In my example the first object has 3 components, the scond has 5 and the third has 4 components. I want iterate though each of them and get thier component name and value.

    The following code does exactly that.

    import org.json.JSONArray;
    import org.json.JSONObject;
    
    public class Foo
    {
      public static void main(String[] args) throws Exception
      {
        String jsonInput = "{\"JObjects\":{\"JArray1\":[{\"A\":\"a\",\"B\":\"b\",\"C\":\"c\"},{\"A\":\"a1\",\"B\":\"b2\",\"C\":\"c3\",\"D\":\"d4\",\"E\":\"e5\"},{\"A\":\"aa\",\"B\":\"bb\",\"C\":\"cc\",\"D\":\"dd\"}]}}";
    
        // "I want to iterate though the objects in the array..."
        JSONObject outerObject = new JSONObject(jsonInput);
        JSONObject innerObject = outerObject.getJSONObject("JObjects");
        JSONArray jsonArray = innerObject.getJSONArray("JArray1");
        for (int i = 0, size = jsonArray.length(); i < size; i++)
        {
          JSONObject objectInArray = jsonArray.getJSONObject(i);
    
          // "...and get thier component and thier value."
          String[] elementNames = JSONObject.getNames(objectInArray);
          System.out.printf("%d ELEMENTS IN CURRENT OBJECT:\n", elementNames.length);
          for (String elementName : elementNames)
          {
            String value = objectInArray.getString(elementName);
            System.out.printf("name=%s, value=%s\n", elementName, value);
          }
          System.out.println();
        }
      }
    }
    /*
    OUTPUT:
    3 ELEMENTS IN CURRENT OBJECT:
    name=A, value=a
    name=B, value=b
    name=C, value=c
    
    5 ELEMENTS IN CURRENT OBJECT:
    name=D, value=d4
    name=E, value=e5
    name=A, value=a1
    name=B, value=b2
    name=C, value=c3
    
    4 ELEMENTS IN CURRENT OBJECT:
    name=D, value=dd
    name=A, value=aa
    name=B, value=bb
    name=C, value=cc
    */
    
    • 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 some data and need to create a json file with this structure
I have a json file that I'm trying to get values out of. One
i have a json file with many elements like these: { code : hfuiew89,
I have a JSON file filled with some arrays. I was wondering if anyone
I have a very large .json file on disk. I want to instantiate this
I have json like this (have dictionary and array). I want draw all json
I'd like to ask for some help. I have an external JSON file with
I have a 'simple' scenario: Read some JSON file, Filter or change some of
I have a JSON file which contains a pound symbol in it. This json

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.