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

  • Home
  • SEARCH
  • 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 6343495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:26:55+00:00 2026-05-24T20:26:55+00:00

I have a List<class> that I would like to convert into a json object

  • 0

I have a List<class> that I would like to convert into a json object and then traverse the data out of the json object.

If this were just a List<String> I could just do something like:

JSONObject obj = new JSONObject();
List<String> sList = new ArrayList<String>();
sList.add("val1");
sList.add("val2");

obj.put("list", sList);

Then I could traverse the list like:

JSONArray jArray = obj.getJSONArray("list");
for (int ii = 0; ii < jArray.size(); ii++)
    System.out.println(jArray.getString(ii));

The problem with using the class is that I need to have access to data within each class element of my List<class> and I don’t know how to encode that / traverse it into JSON. Any help would be greatly appreciated.

  • 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-24T20:26:56+00:00Added an answer on May 24, 2026 at 8:26 pm

    Call getJSONObject() instead of getString(). That will give you a handle on the JSON object in the array and then you can get the property off of the object from there.

    For example, to get the property “value” from a List<SomeClass> where SomeClass has a String getValue() and setValue(String value):

    JSONObject obj = new JSONObject();
    List<SomeClass> sList = new ArrayList<SomeClass>();
    
    SomeClass obj1 = new SomeClass();
    obj1.setValue("val1");
    sList.add(obj1);
    
    SomeClass obj2 = new SomeClass();
    obj2.setValue("val2");
    sList.add(obj2);
    
    obj.put("list", sList);
    
    JSONArray jArray = obj.getJSONArray("list");
    for(int ii=0; ii < jArray.length(); ii++)
      System.out.println(jArray.getJSONObject(ii).getString("value"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of class items List<MyClass> I have a seperate object that
I have an initialization class that preloads content into a variable (probably a list
I have a class that contains a list of objects. What's the best way
I have a class that includes a std::list and wish to provide public begin()
I have a class that has an list<Book> in it, and those Book objects
I have a class that wraps List<> I have GetValue by index method: public
I have a class that has an list<Book> in it, and those Book objects
I have a ViewModel class that contains a list of points, and I am
So let's assume I have a class named ABC that will have a list
I have class method that returns a list of employees that I can iterate

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.