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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:14:54+00:00 2026-06-11T00:14:54+00:00

I have some json, with a sequence of entries which look like this (I’ve

  • 0

I have some json, with a sequence of entries which look like this (I’ve simplified it tofocus on relevant issue which is why it looks a little redundant):

{“name”:”ISPC Seychelles”,”company”:{“name”:”ISPC
Seychelles”,”id”:3427640}}

sometimes however the company property is set to null:

{“name”:”Westin Miyako Kyoto”, “company”:null}

I have defined my classes like this (sorry for the public access modifiers, will change these when I get this bit of code working :)) :

class Entry {
  public String name=;
  public Company company;
  public String toString() {
      return  name + ";" + company;
  }
}

class Company {
    public String name;
    public int id;
    public String toString() {
        return "Company: " + name;
    }
}

In my code, I try to use gson to deserialise the JSON contained in the variable called output:

while ((output = br.readLine()) != null) {
    Gson gson = new Gson();
    Reply reply = gson.fromJson(output, Reply.class); 
    Entry[] entries = reply.entries;
    for (int i=0; i < entries.length; i++) {
        System.out.print(entries[i]+ "\n") ;
    }
}

This works fine when the company element is populated as in the first example in the JSON:

ISPC Seychelles;Company: ISPC Seychelles

However, if the element is not populated as in the second example, I get the following output:

Westin Miyako Kyoto; null

What I would like is the following output:

Westin Miyako Kyoto; Company: null

  • 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-11T00:14:55+00:00Added an answer on June 11, 2026 at 12:14 am

    The toString() representation of Company is controlled by the Company object, so there is no way you can get the representation you want if the Company object is null. The only way to do it is to have something like the following in your Entry class’ toString():

    class Entry {
      ...
      ...
    
        public String toString() {
            return  name + "; " + (company != null ? company : "Company: null");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some JSON which looks generally like this... {appJSON: [ { title:Application Title,
Inside my .aspx I have some JSON code that looks like this: function someFunctionName()
I have some JSON data that looks like this: { 910719: { id: 910719,
Greetings all, I have some JSON code that looks like this: { playlist: [
I have some JSON I am parsing that looks like this: { dhkplhfnhceodhffomolpfigojocbpcb: {
I have some JSON that looks like: groups: [ group_id: 8, group_name: Building, group_color:
I have some JSON that is sent to my webservice that looks something like
I have some JSON returned to the browser like this product: { Title: School
I have some json like this: [{ id: 20148324, teacher, Mr Jones, names: [john,
I have a Django database, in which I have some JSON stored. I wish

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.