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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:59:46+00:00 2026-06-12T18:59:46+00:00

I have the following Item class: public class Item { public Object item; }

  • 0

I have the following Item class:

public class Item {
    public Object item;
}

I am inserting a JSON into this object using GSON.

tmp =

{
    "_id": {
        "$oid": "5076371389d22e8906000000"
    },
    "item": {
        "values": [
            {
                "value1": [
                    4958,
                    3787,
                    344
                ],
                "value2": [
                    4,
                    13,
                    23
                ]
            }
        ], 
        "name": "item1"
    }
}

Java bit:

Item item = new Item();
Gson g = new Gson();
it = g.fromJson(tmp.toString(), Item.class);

it.item becomes a StringMap type (http://code.google.com/p/google-gson/source/browse/trunk/gson/src/main/java/com/google/gson/internal/StringMap.java?r=1131)

I now need to access the sub-objects within this object.
I can use the overridden toString function this type has which prints all objects within this object. But how would I be able to navigate through it?
P.S. The reason I put everything into an object datatype not a structured class is that the JSON structure varies each time, so I can’t really have a class schema.
Any suggestions?

  • 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-12T18:59:47+00:00Added an answer on June 12, 2026 at 6:59 pm

    You should create an object structure that reflects the JSON instead (since this is what you’re trying to do anyway). For your example, you could use this:

    public class MyObject {
        private Item item;
        private String _id;
    
        // getters, setters, etc.
    }
    
    public class Item {
        private List<Value> values;
        private String name;
    
        // getters, setters, etc.
    }
    
    public class Value {
        private List<Integer> values1;
        private List<Integer> values2;
    
        // getters, setters, etc.
    }
    

    Then pass MyObject.class to Gson:

    MyObject myObj = g.fromJson(tmp.toString(), MyObject.class);
    

    You can get the lists in values like so:

    List<Integer> values1 = myObj.getItem().getValues().get(0).getValues1();
    List<Integer> values2 = myObj.getItem().getValues().get(0).getValues2();
    

    Try that and see if it works.

    Also, you should check out my answer to a similar question here, specifically the part at the end about how to write an object structure for Gson based on some JSON object.

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

Sidebar

Related Questions

In C# I have the following object: public class Item { } public class
I have the following: class Item { public object ItemId {get;set;} } class StringItem
I have a List of the following object : public class Item { public
I have a class like the following: class Positive(object): def __init__(self, item): self._validate_item(item) self.item
I have a list of the following object: public class userReview { public long
I have the following classes: public class BaseContainer { public BaseItem item {get; set;}
I have the following code: public class Navigation { public Navigation() { SubNavigation =
If I have the following model; public List<RecommendedProduct> recommendations Then public class RecommendedProduct public
I have the following class: public class DocumentCompare { public string Customer; public string
Let us have the following code: public class TestGenerics { static <T> void mix(ArrayList<T>

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.