【问题标题】:Add additional attributes to JSON response in Java在 Java 中向 JSON 响应添加附加属性
【发布时间】:2021-05-05 13:32:02
【问题描述】:

我有一项服务返回 JSON 格式的列表。 请找到以下代码:

public List<SampleList> getValues() {
        List<SampleList> sample = null;
        sample= DAOFactory.sampleDAO.findByCriteria().add(Restrictions.isNull("endDate")).list();
        return sample;
    }

Class SampleList.java
public class SampleList {
    private Integer categoryId;
    private String  categoryName;
//getter setter 
}

现在我的服务正在返回如下所示的 JSON

{
categoryId : 1,
categoryName : "Test"
}

但我需要在此处封装另一个列表。 Iw ant 低于输出

{
categoryId : 1,
categoryName : "Test"
subCategory:
 {
  name: ""
 } 
}

对于subCategory 属性,我有另一个类似于SampleList.java 的类。我可以得到每个类别对应的子类别。任何人都可以帮助我获得预期的响应吗?

我不想碰我的 SampleList 类。

【问题讨论】:

    标签: java json spring spring-boot servlets


    【解决方案1】:

    你必须扩展你的类 SampleList

    Class SampleList.java
    public class SampleList {
        private Integer categoryId;
        private String  categoryName;
        private SubCategory subCategory;
    //getter setter 
    

    当然,在您返回您的列表之前,您必须在您的 SampleList 项目中设置正确的子类别。

    如果您不想破坏您的 SampleList 类,当然您可以添加一层 DTO 对象并在它们之间进行映射或直接操作响应,例如ResponseBodyAdvice

    【讨论】:

    • 是的,我不想破坏我的 SampleList 类。
    【解决方案2】:

    方法:1

    public class SampleList
    {
        private Integer categoryId;
        private String categoryName;
    
        // Getter and Setter
    }
    
    public class SampleList2
    {
       private String name;
       // Getter and Setter
    }
    

    // 不映射两个不同类的逻辑获取JSON值

    private void getJsonValue() throws JsonProcessingException, JSONException
    {
        SampleList sampleList = new SampleList();
        sampleList.setCategoryId(1);
        sampleList.setCategoryName("cat 1");
        
        String sampleListJson = new ObjectMapper().writeValueAsString(sampleList);
        
        SampleList2 sampleList2 = new SampleList2();
        sampleList2.setName("Sub category");
        
        String valueOfSample2 = new ObjectMapper().writeValueAsString(sampleList2);
        
        JSONObject sampleListJsonObj = new JSONObject(sampleListJson); // for class SampleList
        JSONObject sampleList2JsonObj = new JSONObject(valueOfSample2); // for class SampleList2
        
        sampleListJsonObj.put("subCategory", sampleList2JsonObj);
        
        System.out.println(sampleListJsonObj.toString());
    }
    

    方法:2

     public class SampleList
    {
        private Integer categoryId;
        private String categoryName;
        private SampleList2 subCategory;
    
        // Getter and Setter
    }
    
    public class SampleList2
    {
       private String name;
       // Getter and Setter
    }
    

    // 上面提到的映射两个类的逻辑

    private static void getJsonValue() throws JsonProcessingException
    {
        SampleList sampleList = new SampleList();
        sampleList.setCategoryId(1);
        sampleList.setCategoryName("cat 1");
        
        SampleList2 sampleList2 = new SampleList2();
        sampleList2.setName("Sub category");
        
        sampleList.setSubCategory(sampleList2);
        
        String jsonString = new ObjectMapper().writeValueAsString(sampleList);
        
        System.out.println(jsonString.toString());
    }
    

    如果您对此有任何疑问,请告诉我。

    谢谢。

    【讨论】:

    • 您好苛刻,感谢您提供如此详细的答案。在方法 1 中: JSONObject sampleListJsonObj = new JSONObject(sampleListJson); JSONObject只把Map作为参数而不是String,那么这里可以做什么呢?
    • 我使用了 org.json.JSONObject 并且在它的构造函数中,我们可以传递字符串。请转至stleary.github.io/JSON-java/org/json/…
    • 感谢@harsh_jani 使用这两种方法。
    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2020-03-09
    • 2011-06-12
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    相关资源
    最近更新 更多