Json字符串解析为具有多个对象的java对象

发布于 2024-10-31 23:49:36 字数 2239 浏览 0 评论 0原文

尝试使用 gson 将以下 json 字符串解析为 java 对象

{
    "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 
                    } 
                ] 
            } 
        ]
    }
}

我有我的 java 类,因为

 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

转换后我得到 Prompt 对象为 null。

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

有人可以纠正我吗

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

格子衫的從容 2024-11-07 23:49:36

尝试这个 JSON:

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

使用这个 Java 类:

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;
            }
        }
    }
}

像魅力一样工作

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);
}

输出:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文