{"perListAll":[{"children":[{"children":[],"iconCls":null,"id":2,"perCategory":1,"perFather":null,"perMark":null,"perNumber":"1001","perShowOrder":1,"state":null,"text":"用户管理","url":"yh.jsp"}],"iconCls":null,"id":1,"perCategory":1,"perFather":null,"perMark":"系统管理","perNumber":"1000","perShowOrder":1,"state":null,"text":"系统管理","url":null}]}
easyUI中 json格式必须为
[{"children":[{"children":[],"iconCls":null,"id":2,"perCategory":1,"perFather":null,"perMark":null,"perNumber":"1001","perShowOrder":1,"state":null,"text":"用户管理","url":"yh.jsp"}],"iconCls":null,"id":1,"perCategory":1,"perFather":null,"perMark":"系统管理","perNumber":"1000","perShowOrder":1,"state":null,"text":"系统管理","url":null}]
才行,不能带{"perListAll":
但是我用的谷歌json插件
json.put("perListAll", perListAll);出来后就是带{"perListAll":的,请问怎么去除...
还有就是我不要perMark等不必要的字段,用谷歌json插件怎么写..
谷歌json插件是这个: <result-type name="json" class="com.googlecode.jsonplugin.JSONResult" />
java谷歌json插件怎么用
答案:3 悬赏:20 手机版
解决时间 2021-02-21 18:56
- 提问者网友:鼻尖触碰
- 2021-02-20 23:13
最佳答案
- 五星知识达人网友:蕴藏春秋
- 2021-02-21 00:50
Google的GSON组件可以将对象转为json字符串,而且支持 JDK5 的泛型,还有版本的注解、更改字段名称的注解等等。
测试代码如下:
User 用户类:
public class User {
@Expose
private Long id;
@Expose
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
// 测试 JavaBean 的转换
public class Test {
public static void main(String[] args) {
User user = new User();
user.setId(1001L);
user.setName("张三");
GsonBuilder builder = new GsonBuilder();
// 不转换没有 @Expose 注解的字段
builder.excludeFieldsWithoutExposeAnnotation();
Gson gson = builder.create();
String sUser = gson.toJson(user);
System.out.println(sUser);
// {"id":1001,"name":"张三"}
// 将JSON字符串转换为 JavaBean
User user2 = gson.fromJson(sUser, User.class);
System.out.println(user2.getId() + ", " + user2.getName());
// 1001, 张三
}
}
// 测试 HashMap 的转换
public class MapTest {
public static void main(String[] args) {
User user1 = new User();
user1.setId(1001L);
user1.setName("张三");
User user2 = new User();
user2.setId(1002L);
user2.setName("李四");
Map userMap = new HashMap();
userMap.put("user1", user1);
userMap.put("user2", user2);
GsonBuilder builder = new GsonBuilder();
// 不转换没有 @Expose 注解的字段
builder.excludeFieldsWithoutExposeAnnotation();
Gson gson = builder.create();
String sUserMap = gson.toJson(user, new TypeToken>(){}.getType());
System.out.println(sUserMap);
// {"user1":{"id":1001,"name":"张三"},"user2":{"id":1002,"name":"李四"}}
// 将JSON字符串转换为 HashMap
Map userMap2 = (Map)gson.fromJson(sUser, new TypeToken>(){}.getType());
System.out.println(userMap2.get("user1").getName());
// 张三
}
}
测试代码如下:
User 用户类:
public class User {
@Expose
private Long id;
@Expose
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
// 测试 JavaBean 的转换
public class Test {
public static void main(String[] args) {
User user = new User();
user.setId(1001L);
user.setName("张三");
GsonBuilder builder = new GsonBuilder();
// 不转换没有 @Expose 注解的字段
builder.excludeFieldsWithoutExposeAnnotation();
Gson gson = builder.create();
String sUser = gson.toJson(user);
System.out.println(sUser);
// {"id":1001,"name":"张三"}
// 将JSON字符串转换为 JavaBean
User user2 = gson.fromJson(sUser, User.class);
System.out.println(user2.getId() + ", " + user2.getName());
// 1001, 张三
}
}
// 测试 HashMap 的转换
public class MapTest {
public static void main(String[] args) {
User user1 = new User();
user1.setId(1001L);
user1.setName("张三");
User user2 = new User();
user2.setId(1002L);
user2.setName("李四");
Map userMap = new HashMap();
userMap.put("user1", user1);
userMap.put("user2", user2);
GsonBuilder builder = new GsonBuilder();
// 不转换没有 @Expose 注解的字段
builder.excludeFieldsWithoutExposeAnnotation();
Gson gson = builder.create();
String sUserMap = gson.toJson(user, new TypeToken>(){}.getType());
System.out.println(sUserMap);
// {"user1":{"id":1001,"name":"张三"},"user2":{"id":1002,"name":"李四"}}
// 将JSON字符串转换为 HashMap
Map userMap2 = (Map)gson.fromJson(sUser, new TypeToken>(){}.getType());
System.out.println(userMap2.get("user1").getName());
// 张三
}
}
全部回答
- 1楼网友:妄饮晩冬酒
- 2021-02-21 02:37
你试试用js方法转一下:JSON.parse();js中可以将字符串转为json对象。
- 2楼网友:夜余生
- 2021-02-21 01:07
1:你是用hashMap返回的数据,是键值的方式,然后在把这个map返回回去,当然有这个名称的存在
2:你在map中put了其他键值,并且一起返回回去了,所以会有其它参数
要想达到效果,把list转化成json,然后直接使用response把你转化后的json字符串返回即可。
response.getWriter().write(json);
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯