Skip to content

Getter for org.json.JSONArray::myArrayList #672

Closed

Description

@jacekpodgorz

Please add getter to "myArrayList" field in JSONArray.
It will allow further self-processing without the need to use "toList".

Activity

  1. johnjaylward commented on Mar 21, 2022

    @johnjaylward
    Contributor

    There is no reason for us to expose the back end data types used and would limit us possibly changing the implementation in the future if a better option presents itself.

  2. johnjaylward commented on Mar 21, 2022

    @johnjaylward
    Contributor

    What are you trying to accomplish? Perhaps there is a better way to do what you want without exposing the internals?

  3. jacekpodgorz commented on Mar 21, 2022

    @jacekpodgorz
    Author

    Thank you for your answer.

    I create Array:
    [{" x ": 1}, {" x ": 2," y ": 2.5}, {" x ": 3}]
    the variable "myArrayList" is "ArrayList ".

    If use method "toList()" I get an "ArrayList ".

    The variable is garbled. Working on JSONArray using "toList", I will always get a new object.

    It would be helpful to download "Stream".

  4. johnjaylward commented on Mar 21, 2022

    @johnjaylward
    Contributor

    Since JSONArray already implements Iterable and assuming you are using Java8+, you can do something like this:

    JSONArray ja = new JSONArray("[{\"x\": 1}, {\"x\": 2,\"y\": 2.5}, {\"x\": 3}]");
    Stream<Object> myStream = java.util.StreamSupport(ja.spliterator(), false); // true for parallel processing
    // process the stream how you want.

    Docs:

    if you just want to iterate over the JSONArray using oldschool Java7- iterators, you can do that like so:

    JSONArray ja = new JSONArray("[{\"x\": 1}, {\"x\": 2,\"y\": 2.5}, {\"x\": 3}]");
    for(Object item : ja) {
     // do something with each item
    }

    One Caveat with either approach which both use the iterator() method is that it's exposing the raw items stored in the JSONArray.myArrayList (which sounds like what you wanted anyway). But what this means is that if you do any code-built JSONArrays (using the put* methods), you may not get back JSON compliant values as the JSON conversions are only garunteed to happen using the JSONArray.get* methods, which the iterator does not call. (see the put(Object) method).

  5. jacekpodgorz commented on Mar 21, 2022

    @jacekpodgorz
    Author

    Thank you for your answer.

    I understand the objections. I am using old code for this library which has been rewritten previously. Performs multiple operations on "myArrayList" such as sorting. The library was not "Iterable". I want to use the current via Maven.
    Thank you for your help. Regards and I close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions