【问题标题】:Json string parsing to java object with multiple objectsJson字符串解析为具有多个对象的java对象
【发布时间】:2023-03-07 22:46:01
【问题描述】:

尝试使用 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);

有人可以纠正我吗

【问题讨论】:

    标签: java json gson


    【解决方案1】:

    试试这个 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)
    

    【讨论】:

    • 很高兴听到这个消息。如果此答案解决了您的问题,请接受投票
    • 把inner class的定义放到outter class里面就可以解决问题了?
    猜你喜欢
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多