Java 通过usong JackSon将JSON映射到对象

Java 通过usong JackSon将JSON映射到对象,java,json,jackson,jsonobject,Java,Json,Jackson,Jsonobject,exmaple.file 我想将以下json映射到对象。我使用Jackson尝试了自定义对象 [ { "widgetTitle":"Bannerfgfg", "widgetCode" : "PRTIMG001", "widgetType":"dssdsd", "widGetSpanNumber":"2", "jqimagePath":"images/slider/", "jqimageCount":"4", "jqimageHei

exmaple.file

我想将以下json映射到对象。我使用Jackson尝试了自定义对象

[
    {
    "widgetTitle":"Bannerfgfg",
     "widgetCode" : "PRTIMG001",
    "widgetType":"dssdsd",
    "widGetSpanNumber":"2",
    "jqimagePath":"images/slider/",
    "jqimageCount":"4",
    "jqimageHeight":"300"
    },
    {
     "widgetTitle" : "Favourites",
     "widgetCode" : "Favourites",
     "widgetType" : "sdsdsd",                           
     "widgetAttributes":{
        {"key":"loadTemplate","value":[{"url" : "favourites"}]
        },
        {"key":"loadCntId","value": "tobeloaded"}
        }
    },
    {
        "widgetTitle" : "Exchange Rates",           
        "widgetCode" : "sdsdsd",
        "widgetType" : "sdsdsdsdsd",
        "widgetAttributes": {
            {"key":"datatype","value": "json"},
            {"key":"appCode","value": "mst"},
            {"key":"url","value":"exchangeMaster/getCurrencyExchangeDetail?"},
            {"key":"colNames" ,"value":["Form CCY","To CCY","Ex RT","Prd FRM","Prd TO"]},
            {"key":"colModel","value":[ {"name" : "fromCurrencyCode","index" : "fromCurrencyCode","align":"center"},
                        {"name" : "toCurrencyCode","index" : "toCurrencyCode","align":"center"},
                        {"name" : "exchangeRate",   "index" : "exchangeRate","align":"right"},
                        {"name" : "periodFrom","index" : "periodFrom"},                 
                        {"name" : "periodTo","index" : "periodTo"}
            ]},
            {"key":"width","value": "440"},
             {"key":"sortable","value": true},
           {"key": "filterToolbar","value":{ 
                "stringResult": "false", 
                "searchOnEnter": "true",
                 "enableClear": "false"
            }},
             {"key":"jsonReader","value":{
                "root":"currencyExchangeSOs" 
            }},
            {"key":"sortname","value": "fromCurrencyCode"}
        }
    }
]
我使用以下代码将json映射到对象

                    ObjectMapper mapper = new ObjectMapper();
        InputStream src = resource.getInputStream();


         try {
             PortalModal[] portalModal = mapper.readValue(src, PortalModal[].class);
             portalDataList =  Arrays.asList(portalModal);

        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
PortalModal.java

public class PortalModal {

    private String widgetCode;
    private String widgetTitle;
    private String widgetType;
    private Boolean isAuthorize;
    private Map<String,Object> widgetAttributes; 
    private String widGetSpanNumber;
    private String jqimagePath;
    private String jqimageCount;
    private String jqimageHeight;
    private HashMap<String,String> widgetContent;


    public String getWidgetCode() {
        return widgetCode;
    }
    public void setWidgetCode(String widgetCode) {
        this.widgetCode = widgetCode;
    }
    public String getWidgetTitle() {
        return widgetTitle;
    }
    public void setWidgetTitle(String widgetTitle) {
        this.widgetTitle = widgetTitle;
    }
    public String getWidgetType() {
        return widgetType;
    }
    public void setWidgetType(String widgetType) {
        this.widgetType = widgetType;
    }
    public Boolean getIsAuthorize() {
        return isAuthorize;
    }
    public void setIsAuthorize(Boolean isAuthorize) {
        this.isAuthorize = isAuthorize;
    }

    public Map<String, Object> getWidgetAttributes() {
        return widgetAttributes;
    }
    public void setWidgetAttributes(Map<String, Object> widgetAttributes) {
        this.widgetAttributes = widgetAttributes;
    }
    public String getWidGetSpanNumber() {
        return widGetSpanNumber;
    }
    public void setWidGetSpanNumber(String widGetSpanNumber) {
        this.widGetSpanNumber = widGetSpanNumber;
    }
    public String getJqimagePath() {
        return jqimagePath;
    }
    public void setJqimagePath(String jqimagePath) {
        this.jqimagePath = jqimagePath;
    }
    public String getJqimageCount() {
        return jqimageCount;
    }
    public void setJqimageCount(String jqimageCount) {
        this.jqimageCount = jqimageCount;
    }
    public String getJqimageHeight() {
        return jqimageHeight;
    }
    public void setJqimageHeight(String jqimageHeight) {
        this.jqimageHeight = jqimageHeight;
    }
    public HashMap<String, String> getWidgetContent() {
        return widgetContent;
    }
    public void setWidgetContent(HashMap<String, String> widgetContent) {
        this.widgetContent = widgetContent;
    }



}

我不想用simplejson。请帮助我解决此问题。

对于小部件属性,您需要为键/值对象创建一个类,并将映射更改为数组。您的新对象将如下所示:

public class Attribute {
    private String key;
    private String value;

    @JsonCreator
    public Attribute(@JsonProperty("key") String key,
                     @JsonProperty("value") String value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }
}
private List<Attribute> widgetAttributes;
您在
portalModel
上的属性将如下所示:

public class Attribute {
    private String key;
    private String value;

    @JsonCreator
    public Attribute(@JsonProperty("key") String key,
                     @JsonProperty("value") String value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }
}
private List<Attribute> widgetAttributes;
私有列表widgettributes;
列出PortalCatalList;
ObjectMapper mapper=新的ObjectMapper();
Map[]maps=mapper.readValue(
resource.getInputStream(),Map[].class);
List obj1=新的ArrayList();
用于(地图:地图){
obj1.add(map);
}
PortalCatalList=(列表)(列表)obj1;

这段代码运行正常

您拥有的不是有效的JSON(由于widgetAttributes)。无论你使用什么JSON解析器,这都是不可接受的。我同意。我更改了json。。谢谢