工作中需要把浏览器返回的json数据转成公司服务器能够识别的xml文件,这里是用到了hutool工具,以及fastjson进行解析
{
"id": "13",
"name": "OpenDataV",
"canvasData": [
{
"id": "7e181c23e15e4598be25c83566c67ec3",
"component": "Image",
"name": "图片",
"propValue": {},
"style": {
"left": 330,
"top": 220,
"width": 200,
"height": 200,
"rotate": 0,
"background": {
"backgroundColor": "#14c9c9"
},
"backgroundColor": "#14c9c9"
},
"dataIntegrationMode": "SELF"
},
{
"id": "9140a3cc09a24382a5e8ebd43524f9a0",
"component": "DateText",
"name": "时间文本",
"propValue": {
"base": {
"format": "YYYY-MM-DD HH:mm:ss"
}
},
"style": {
"left": 362,
"top": 481,
"width": 200,
"height": 50,
"rotate": 0,
"color": "skyblue",
"fontSize": 20,
"fontWeight": 200,
"fontFamily": "Arial"
},
"dataIntegrationMode": "SELF"
},
{
"id": "de6b19ec73ca44718ea9803e25c839e2",
"component": "ScrollTable",
"name": "滚动表格",
"propValue": {
"header": {
"header": [
"姓名",
"年龄",
"性别"
],
"headerBGC": "#00BAFF",
"headerHeight": 35,
"index": false,
"indexHeader": "#"
},
"rows": {
"data": {
"height": 30,
"oddRowBGC": "#003B51",
"evenRowBGC": "#0A2732"
}
}
},
"style": {
"left": 461,
"top": 629,
"width": 400,
"height": 100,
"rotate": 0
},
"dataIntegrationMode": "UNIVERSAL",
"data": {
"type": "DEMO",
"otherConfig": {}
}
}
],
"canvasStyle": {
"width": 1707,
"height": 1067,
"background": {
"backgroundColor": "#272e3b"
}
}
}
浏览器返回的json文件比较复杂嵌套多个对象以及数组
@Data
@AllArgsConstructor
@NoArgsConstructor
@XmlRootElement(name = "Xml")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Canvas {
private String id;
private String name;
// @XmlTransient
// @XmlAttribute(name = "canvasStyle")
private CanvasStyle canvasStyle;
private String width;
private String height;
public List<CanvasData> getCanvasData() {
return canvasData;
}
# 这里是组件的类型不确定这里进行类型匹配
@XmlElementRefs({@XmlElementRef(name = "text", type = StaticText.class, required = true),
// @XmlElementRef(name = "input", type = Button.class,required = true),
// @XmlElementRef(name = "button", type = Button.class)
})
public void setCanvasData(List<CanvasData> canvasData) {
this.canvasData = canvasData;
}
@JSONField(name = "canvasData")
private List<CanvasData> canvasData;
//@XmlElementRef(type = CanvasStyle.class)
public String getWidth() {
return width;
}
@XmlAttribute(name ="width" )
public void setWidth(String width) {
this.width =this.canvasStyle.getWidth() ;
}
public String getHeight() {
return height;
}
@XmlAttribute(name ="height" )
public void setHeight(String height) {
this.height = this.canvasStyle.getHeight();
}
public String getId() {
return id;
}
@XmlAttribute(name = "id")
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}@XmlAttribute(name = "name")
public void setName(String name) {
this.name = name;
}
public CanvasStyle getCanvasStyle() {
return canvasStyle;
}
public void setCanvasStyle(CanvasStyle canvasStyle) {
this.canvasStyle = canvasStyle;
}
}
我把抽象类改成了类文件,因为抽象fastjson解析为null,为hutool的解析我试过无法解析直接报错,也有可能我的使用方式不正确
@Data
@XmlRootElement
public class CanvasData implements Serializable {
private String id;
private String component;
private String name;
private PropValue proValue;
private Style style;
private String dataIntegrationMode;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class CanvasStyle {
@XmlAttribute
private String width;
@XmlAttribute
private String height;
//@XmlElementRef(type = BackGround.class)
// private BackGround background;
}
这里列举一个,这个需要在主类型中类型匹配中添加一项
@Data
@XmlRootElement(name = "StaticText")
public class StaticText extends CanvasData{
}
public class JsonTest {
@Test
void JsonTest(){
String url = "D:\\code2\\ParseJsonToXML\\src\\main\\resources" + File.separator + "hyway.xml";
JSON json = JSONUtil.readJSON(new File("D:\\code2\\ParseJsonToXML\\src\\main\\resources\\test.json"), Charset.defaultCharset());
String s = json.toString();
JSONObject entries = JSONUtil.parseObj(json);
Canvas canvas = com.alibaba.fastjson.JSON.parseObject(String.valueOf(json), Canvas.class);
System.out.println(canvas);
XmlUtil.uploadByBeanToXml(canvas, Canvas.class,"utf-8",url);
}
}
使用@JSONField或者使用@JSONType注解
@JSONType(orders = {"top","left","width","height","rotate","color","fontSize","fontWeight","fontFamily"})
或者是
package com.hyway.domain;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;
@Data
public class JsonStyle {
@JSONField(ordinal = 1)
private Integer left;
@JSONField(ordinal = 2)
private Integer top;
@JSONField(ordinal = 3)
private Integer width;
@JSONField(ordinal = 4)
private Integer height;
@JSONField(ordinal = 5)
private Integer rotate;
@JSONField(ordinal = 6)
private BackGround background;
@JSONField(ordinal = 7)
private String color;
@JSONField(ordinal = 8)
private Integer fontSize ;
@JSONField(ordinal = 9)
private Integer fontWeight;
@JSONField(ordinal = 10)
private String fontFamily;