json字符串转为Java对象请教
Json字符串格式为:[{'handleName':'查看','imgsrc':'../../images/folder_open.png','href':'initEd...
Json字符串格式为:[{'handleName':'查看','imgsrc':'../../images/folder_open.png','href':'initEditRole.act','args':'[{'argName':'role.id','propertyName':'id'}]','onclick':''},{'handleName':'删除','imgsrc':'../../images/folder_delete.png','href':'deletesRole.act','args':'[{'argName':'role.id','propertyName':'id'}]','onclick':'javascript:return confirm(\'是否确定删除?\');'}]
Json转换方法为:
public static Object getDTO(String jsonString, Class clazz, Map map){
JSONObject jsonObject = null;
try{
setDataFormat2JAVA();
jsonObject = JSONObject.fromObject(jsonString);
}catch(Exception e){
e.printStackTrace();
}
return JSONObject.toBean(jsonObject, clazz, map);
}
private static void setDataFormat2JAVA(){
//设定日期转换格式
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"}));
}
实体类为:
public class Handles {
private String handleName;
private String imgsrc;
private String href;
private List<HandleArg> args;
private String onclick;
public String getHandleName() {
return handleName;
}
public void setHandleName(String handleName) {
this.handleName = handleName;
}
public String getImgsrc() {
return imgsrc;
}
public void setImgsrc(String imgsrc) {
this.imgsrc = imgsrc;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public List<HandleArg> getArgs() {
return args;
}
public void setArgs(List<HandleArg> args) {
this.args = args;
}
public String getOnclick() {
return onclick;
}
public void setOnclick(String onclick) {
this.onclick = onclick;
}
@Override
public String toString() {
return "Handles [args=" + args + ", handleName=" + handleName
+ ", href=" + href + ", imgsrc=" + imgsrc + ", onclick="
+ onclick + "]";
}
}
public class HandleArg {
private String argName;
private String propertyName;
public String getArgName() {
return argName;
}
public void setArgName(String argName) {
this.argName = argName;
}
public String getPropertyName() {
return propertyName;
}
public void setPropertyName(String propertyName) {
this.propertyName = propertyName;
}
}
异常信息为: 展开
Json转换方法为:
public static Object getDTO(String jsonString, Class clazz, Map map){
JSONObject jsonObject = null;
try{
setDataFormat2JAVA();
jsonObject = JSONObject.fromObject(jsonString);
}catch(Exception e){
e.printStackTrace();
}
return JSONObject.toBean(jsonObject, clazz, map);
}
private static void setDataFormat2JAVA(){
//设定日期转换格式
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"}));
}
实体类为:
public class Handles {
private String handleName;
private String imgsrc;
private String href;
private List<HandleArg> args;
private String onclick;
public String getHandleName() {
return handleName;
}
public void setHandleName(String handleName) {
this.handleName = handleName;
}
public String getImgsrc() {
return imgsrc;
}
public void setImgsrc(String imgsrc) {
this.imgsrc = imgsrc;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public List<HandleArg> getArgs() {
return args;
}
public void setArgs(List<HandleArg> args) {
this.args = args;
}
public String getOnclick() {
return onclick;
}
public void setOnclick(String onclick) {
this.onclick = onclick;
}
@Override
public String toString() {
return "Handles [args=" + args + ", handleName=" + handleName
+ ", href=" + href + ", imgsrc=" + imgsrc + ", onclick="
+ onclick + "]";
}
}
public class HandleArg {
private String argName;
private String propertyName;
public String getArgName() {
return argName;
}
public void setArgName(String argName) {
this.argName = argName;
}
public String getPropertyName() {
return propertyName;
}
public void setPropertyName(String propertyName) {
this.propertyName = propertyName;
}
}
异常信息为: 展开
4个回答
2013-02-05
展开全部
用这个JSON试试
[{'handleName':'查看','imgsrc':'../../images/folder_open.png','href':'initEditRole.act','args':[{'argName':'role.id','propertyName':'id'}],'onclick':''},{'handleName':'删除','imgsrc':'../../images/folder_delete.png','href':'deletesRole.act','args':[{'argName':'role.id','propertyName':'id'}],'onclick':'javascript:return confirm(\'是否确定删除?\');'}]
在[]外面不应该有引号
[{'handleName':'查看','imgsrc':'../../images/folder_open.png','href':'initEditRole.act','args':[{'argName':'role.id','propertyName':'id'}],'onclick':''},{'handleName':'删除','imgsrc':'../../images/folder_delete.png','href':'deletesRole.act','args':[{'argName':'role.id','propertyName':'id'}],'onclick':'javascript:return confirm(\'是否确定删除?\');'}]
在[]外面不应该有引号
追问
已经取掉,但是还是不行
追答
要不你用Gson试试,它有fromJson方法,比如
protected T fromInputJson(Class classOfT) throws Exception {
InputStream is = request.getInputStream();
String input = readInputStream(is);
T result = gson.fromJson(input, classOfT);
if (result == null) {
log.error("Bad Request");
}
return result;
}
展开全部
:[{'handleName':'查看'
要用双引号 “handleName”:“查看”
如String s="[{“/handleName”/:“/查看”/}]"
要用双引号 “handleName”:“查看”
如String s="[{“/handleName”/:“/查看”/}]"
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
数组的话用JSONArray这个类来处理不是JSONObject
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
加【】一个是数组
更多追问追答
追问
我就是想把args作为一个数组解析,在我的解释对应的实体类的时候已经把args定义为List集合;想请教到底我这个问题出现在哪里?
追答
json就是把java封装成数组,转换就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询