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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:06:31+00:00 2026-06-17T13:06:31+00:00

I am facing a problem , sometimes Json response returns an array of objects

  • 0

I am facing a problem , sometimes Json response returns an array of objects , sometimes object itself , how we can handle dynamically in the response class.
In the current eg :results sometimes gets an array of objects

 "\"results\": " +
            "[{" +

and sometimes object itself

 "\"results\": " +
            "{" +

Eg:

How we can handle this ?

Gson gson = new Gson();
    SearchResponse response=new SearchResponse();
    response= gson.fromJson("{" +
            "\"completed_in\": 0.047," +
            "\"max_id\": 291771567376039936," +
            "\"max_id_str\": \"291771567376039936\"," +
            "\"next_page\": \"?page=2&max_id=291771567376039936&q=javacodegeeks\"," +
            "\"page\": 1," +
            "\"query\": \"javacodegeeks\"," +
            "\"refresh_url\": \"?since_id=291771567376039936&q=javacodegeeks\"," +
            "\"results\": " +
            "{" +
            "\"created_at\": \"Thu, 17 Jan 2013 04:58:57 +0000\"," +
            "\"from_user\": \"hkokko\"," +
            "\"from_user_id\": 24726686," +
            "\"from_user_id_str\": \"24726686\"," +
            " \"from_user_name\": \"Hannu Kokko\"," +
            " \"geo\": null," +
            "\"id\": 291771567376039936," +
            "\"id_str\": \"291771567376039936\"," +
            "\"iso_language_code\": \"en\"," +
            " \"metadata\": {" +
            "\"result_type\": \"recent\"}," +
            "\"profile_image_url\": \"hjh\"," +
            "\"profile_image_url_https\": \"kkj\"," +
            "\"source\": \"<a href="hj;\"," +
            "\"text\": \"Continuous Deployment: Are You Afraid It Might Work? jh\"," +
            "\"to_user\": null," +
            "\"to_user_id\": 0," +
            "\"to_user_id_str\": \"0\"," +
            "\"to_user_name\": null" +
            " }," +
            "\"results_per_page\": 15," +
            "\"since_id\": 0," +
            "\"since_id_str\": \"0\"" +
            "}", SearchResponse.class);
    System.out.println(response.toString());

Kindly assist…

Can anyone give any suggestions by using different jars to achieve this?

  • 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-17T13:06:32+00:00Added an answer on June 17, 2026 at 1:06 pm

    i found a solution for this ,i felt to share this..The code will automatically convert ..if excepted response is arraylist in response class….then if object is coming in response then add to arraylist else if arraylist it will take the same list.
    we need hook change the response bfore it calls fromJson.

    public class ArrayAdapter<T> extends TypeAdapter<List<T>> {
        private Class<T> adapterclass;
    
        public ArrayAdapter(Class<T> adapterclass) {
    
            this.adapterclass = adapterclass;
        }
    
        public List<T> read(JsonReader reader) throws IOException {
    
    
            List<T> list = new ArrayList<T>();
    
            Gson gson = new Gson();
    
            if (reader.peek() == JsonToken.BEGIN_OBJECT) {
    
                T inning = (T) gson.fromJson(reader, adapterclass);
                list.add(inning);
    
            } else if (reader.peek() == JsonToken.BEGIN_ARRAY) {
    
                reader.beginArray();
                while (reader.hasNext()) {
                    T inning = (T) gson.fromJson(reader, adapterclass);
                    list.add(inning);
                }
                reader.endArray();
    
            } else {
                reader.skipValue();
            }
    
            return list;
        }
    
        public void write(JsonWriter writer, List<T> value) throws IOException {
    
        }
    
    }
    
    public class ArrayAdapterFactory implements TypeAdapterFactory {
    
      @SuppressWarnings({ "unchecked" })
      @Override
      public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
    
          ArrayAdapter typeAdapter = null;
          try {
              if (type.getRawType() == List.class)
              {
    
                  typeAdapter = new ArrayAdapter(
                          (Class) ((ParameterizedType) type.getType())
                                  .getActualTypeArguments()[0]);
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
    
          return typeAdapter;
    }
    

    then just call

     Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ArrayAdapterFactory()).create();
     SearchResponse response;
     esponse= gson.fromJson("your json string", SearchResponse.class)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm always facing a problem where I can't really think of service object encapsulating
iam facing problem in passing array to view. this is my controller code.. function
These days I'm facing a problem with encoding string from an array of byte.
i am facing problem whenever i am running my wsdl2java the ExtensionMapper class is
I'm using jQuery masonry and facing a problem, the margin between boxes (vertically) sometimes
I'm working with XML files, and sometimes I'm facing with an XML encoding problem,
I am facing a problem with UITextField. It crashing the app (sometimes, not all
I am facing a weird problem while initializes javascript date object,no matter what I
Sometime ago I was facing the problem the other way around: how to drag
Iam facing problem in understanding and converting a matlab code into opencv. I want

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.