3

Trying to parse the following json string to java object using gson

{
    "entry": "132456",
    "product": {
        "item": "123456",
        "prompts": [
            {
                "promptId": "1",
                "promptNumber": "109",
                "promptType": 4,
                "promptTypeDesc": "desc1",
                "validations": [
                    {
                        "minLen": 10,
                        "maxLen": 10,
                        "required": true
                    } 
                ] 
            },
            {
                "promptId": "2",
                "promptNumber": "110",
                "promptType": 4,
                "promptTypeDesc": "desc2",
                "validations": [
                    {
                        "minLen": 12,
                        "maxLen": 12,
                        "required": true
                    } 
                ] 
            },
            {
                "promptId": "3",
                "promptNumber": "72",
                "promptType": 5,
                "promptTypeDesc": "desc4",
                "validations": [
                    {
                        "required": true 
                    } 
                ] 
            } 
        ]
    }
}

I have my java classes as

 public class Info{
        private String entry;
        private Product product;
       // added setters and getters

  /* Product is inner class*/
  public static Product {
      private String item;
      private List<Prompt> prompts;
     // added getters and setters

     /*Prompt inner class*/
     public static Prompt{
        private String promptId;
        private String promptNumber;
        private List<Validation> validations;
        // getters and setters


      /*Validation inner class*/
      public static Validation{
          private int maxLen;
          private int minLen;
          private boolean required;
          // added getters and setters
      } // end of Validation class
    } // end of Prompt class
   } // end of Product
} // End of Info

I am getting Prompt object as null after converting.

       item = gson.fromJson(response, Info.class);

Can someone please correct me

1 Answer 1

3

Try this JSON:

{
    "entry": "132456",
    "product": 
    {
        "item": "123456",
        "prompts": 
        [
            {
                "promptId": "1",
                "promptNumber": "109",
                "promptType": 4,
                "promptTypeDesc": "desc1",
                "validations":
                [
                    {
                        "minLen": 10,
                        "maxLen": 10,
                        "required": true 
                    } 
                ] 
            } 
        ] 
    }
}

With this Java Class:

import java.util.List;

public class Info {

    private String entry;
    private Product product;

    public class Product {

        private String item;
        private List<Prompt> prompts;

        public class Prompt {

            private String promptId;
            private String promptNumber;
            private int promptType;
            private String promptTypeDes;
            private List<Validation> validations;

            public class Validation {

                private int maxLen;
                private int minLen;
                private boolean required;
            }
        }
    }
}

Works like a charm

public static void main(String args[]){

    String input = "{"
            + "\"entry\": \"132456\","
            + "\"product\": {\"item\": \"123456\","
            + "\"prompts\":[{\"promptId\": \"1\","
            + "\"promptNumber\": \"109\","
            + "\"promptType\": 4,"
            + "\"promptTypeDesc\": \"desc1\","
            + "\"validations\":[{\"minLen\": 10,"
            + "\"maxLen\": 10"
            + ",\"required\": true}]}]}}";

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    Info item = gson.fromJson(input, Info.class);

    String jsonOutput = gson.toJson(item);
    System.out.println(jsonOutput);
}

Output:

run:
{
  "entry": "132456",
  "product": {
    "item": "123456",
    "prompts": [
      {
        "promptId": "1",
        "promptNumber": "109",
        "promptType": 4,
        "validations": [
          {
            "maxLen": 10,
            "minLen": 10,
            "required": true
          }
        ]
      }
    ]
  }
}
BUILD SUCCESSFUL (total time: 0 seconds)
Sign up to request clarification or add additional context in comments.

1 Comment

Just put the inner class's definition into the outter class to solve the issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.