有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用映射器从包含多个引用单元的JSON字符串中获取对象列表?

我的要求是获取所有选定列并将其作为Json字符串保存到数据库中,然后再次将它们转换回对象列表并返回到UI, 我的Json看起来像这样,我想要维度列表,我如何做到这一点?请推荐我

{
    "dimensions": [{
        "displayName": "Advertised Products",
        "name": "Product",
        "selectable": false,
        "multipleSelect": true,
        "hasFavorites": true,
        "showCancel": false,
        "showClear": false,
        "hasCustomObjects": true,
        "isPrimary": true,
        "searchable": true,
        "editable": true
    }, {
        "displayName": "Companion Products",
        "name": "Product",
        "selectable": false,
        "multipleSelect": true,
        "hasFavorites": true,
        "showCancel": false,
        "showClear": false,
        "hasCustomObjects": true,
        "isPrimary": true,
        "searchable": true,
        "editable": true
    }, {
        "displayName": "Fsp Products",
        "name": "Product",
        "selectable": false,
        "multipleSelect": true,
        "hasFavorites": true,
        "showCancel": false,
        "showClear": false,
        "hasCustomObjects": true,
        "isPrimary": true,
        "searchable": true,
        "editable": true
    }]
}

共 (1) 个答案

  1. # 1 楼答案

    创建具有维度对象引用、setter和getter的DimensionTO pojo

        public class DimensionTO {
        private Dimension dimension;
    
        public Dimension getDimension () {
            return dimension;
        }
    
        public void setUser(Dimension dimension) {
            this.dimension= dimension;
        }
    
    }
    

    使用对象映射器读取尺寸列表

    final ObjectMapper mapper = new ObjectMapper();
    WatcherTO[] dimensionList = mapper.readValue(passYourJson, WatcherTO[].class);
    

    现在我们可以迭代dimensionList以获得每个维度对象的值