我想用类似于这个结构的java对象创建一个json文件:
{“用户”:
{"1" : { "ids" : [1,2,3], "names" : ["anton","berta","charlie"] }, {"2" : { "ids" : [4,5,6], "names" : ["dora","emil","friedrich"] },
......等等。
我的问题是我不知道如何生成第二层次的数字。在所有教程中,我发现层次结构名称是由类名称或注释生成的,但我不能用这种方式来创建我请求的名称。
有没有办法这样做而不用写一个巨大的方法,我手工生成json Stirng?
格尔茨
答案 0 :(得分:0)
让我们以Gson为例:
您可以使用Gson自定义序列化程序如下(我创建类Users,否则其他List将使用MySerializer,这是不期望的)
import com.google.common.collect.Lists;
import com.google.gson.*;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static class User {
private List<Integer> ids;
private List<String> names;
public User(List<Integer> ids, List<String> names) {
this.ids = ids;
this.names = names;
}
}
public static class Users extends ArrayList<User> {
}
public static void main(String[] args) {
Users users = new Users();
users.add(new User(Lists.newArrayList(1, 2, 3), Lists.newArrayList("anton", "berta", "charlie")));
users.add(new User(Lists.newArrayList(4, 5, 6), Lists.newArrayList("dora", "emil", "friedrich")));
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Users.class, new MySerializer());
System.out.printf(gsonBuilder.create().toJson(users));
}
public static class MySerializer implements JsonSerializer<Users> {
@Override
public JsonElement serialize(Users users, Type type, JsonSerializationContext jsonSerializationContext) {
JsonObject jsonObject = new JsonObject();
for (int i = 0; i < users.size(); i++) {
jsonObject.add(String.valueOf(i + 1), jsonSerializationContext.serialize(users.get(i)));
}
return jsonObject;
}
}
}
另一个简单的方法是将列表放入map中,然后生成json,如
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Test {
public static class User {
private List<Integer> ids;
private List<String> names;
public User(List<Integer> ids, List<String> names) {
this.ids = ids;
this.names = names;
}
}
public static void main(String[] args) {
List<User> users = new ArrayList<User>();
users.add(new User(Lists.newArrayList(1, 2, 3), Lists.newArrayList("anton", "berta", "charlie")));
users.add(new User(Lists.newArrayList(4, 5, 6), Lists.newArrayList("dora", "emil", "friedrich")));
Map<String, User> map = new LinkedHashMap<String, User>();
for (int i = 0; i < users.size(); i++) {
map.put(String.valueOf(i + 1), users.get(i));
}
System.out.printf(new Gson().toJson(map));
}
}
答案 1 :(得分:0)
创建每个json条目所需的代码并不多:
public static String toJson(int i, List<User> users) {
StringBuilder sb = new StringBuilder("{\"").append(i).append("\" : { \"ids\" : [");
for (User user : users)
sb.append(user.getId()).append(",");
sb.setCharAt(sb.length() - 1, ']');
sb.append(", \"names\" : [\"");
for (User user : users)
sb.append(user.getName()).append("\",\"");
return sb.replace(sb.length() - 2, sb.length(),"] } ,").toString();
}
使用此方法,在您的时间段内向列表中添加新用户并为每个列表分配序号是一件小事。
这是一些完整的测试代码及其输出:
static class User {
private final int id;
private final String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
public static String toJson(int i, List<User> users) {
StringBuilder sb = new StringBuilder("{\"").append(i).append("\" : { \"ids\" : [");
for (User user : users)
sb.append(user.getId()).append(",");
sb.setCharAt(sb.length() - 1, ']');
sb.append(", \"names\" : [\"");
for (User user : users)
sb.append(user.getName()).append("\",\"");
return sb.replace(sb.length() - 2, sb.length(),"] } ,").toString();
}
public static void main(String[] args) {//throws Exception {
List<User> users = new ArrayList<User>();
users.add(new User(1, "anton"));
users.add(new User(2, "berta"));
users.add(new User(3, "charlie"));
System.out.println(toJson(1, users));
}
输出:
{"1" : { "ids" : [1,2,3], "names" : ["anton","berta","charlie"] } ,