NutzCN Logo
问答 Json解析pojo不正常
发布于 2497天前 作者 lambert 2331 次浏览 复制 上一个帖子 下一个帖子
标签:

大神,过年好,又有问题来麻烦你了。今天在使用json解析的时候,发现下列json,不能被正常解析:

{
	"categoryPathList": [
		[{
			"name": "床品"
		}, {
			"name": "床垫"
		}],
		[{
			"name": "厨房"
		}, {
			"name": "铁筷"
		}],
		[{
			"name": "休闲饮食"
		}, {
			"name": "零食"
		}],
		[{
			"name": "大头"
		}, {
			"name": "茶几"
		}]
	]
}

我的pojo结构:

public class CategoryPathList {
    private String name;
}
-----------------------------------------
public class Root {
    private List<CategoryPathList> categoryPathList;
}

日志会打出来:

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Disconnected from the target VM, address: '127.0.0.1:50377', transport: 'socket'
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map
	at org.nutz.mapl.impl.convert.ObjConvertImpl.injectObj(ObjConvertImpl.java:192)
	at org.nutz.mapl.impl.convert.ObjConvertImpl.inject(ObjConvertImpl.java:81)
	at org.nutz.mapl.impl.convert.ObjConvertImpl.injectCollection(ObjConvertImpl.java:169)
	at org.nutz.mapl.impl.convert.ObjConvertImpl.inject(ObjConvertImpl.java:75)
	at org.nutz.mapl.impl.convert.ObjConvertImpl.convert(ObjConvertImpl.java:65)
	at org.nutz.mapl.Mapl.maplistToObj(Mapl.java:34)
	at org.nutz.mapl.impl.convert.ObjConvertImpl.injectObj(ObjConvertImpl.java:220)
	at org.nutz.mapl.impl.convert.ObjConvertImpl.inject(ObjConvertImpl.java:81)
	at org.nutz.mapl.impl.convert.ObjConvertImpl.convert(ObjConvertImpl.java:65)
	at org.nutz.mapl.Mapl.maplistToObj(Mapl.java:34)
	at org.nutz.json.Json.parse(Json.java:79)
	at org.nutz.json.Json.fromJson(Json.java:58)
	at org.nutz.json.Json.fromJson(Json.java:163)
5 回复
package com.timemans.plus.domain.productInfo;

import lombok.Data;

import java.util.List;

@Data
public class Root {
    
    private List<CategoryPathList> categoryPathList;

}
-----------------------------------
package com.timemans.plus.domain.productInfo;

import lombok.Data;

@Data
public class CategoryPathList {
    private String name;
}

调用时,Root l = Json.fromJsonAsList(Root.class, result);

结贴了,是结构上应该是:private List<List> categoryPathList; 这样就对了

添加回复
请先登陆
回到顶部