我有两个普通的pojo对象:
例如:
class person{
private String name;
private String id;
}
class address{
private String homeaddress;
private String officeaddress;
}
如何使用Gson库创建JSON文件,如下所示:
{
"person": [{name:"test",id:1}]
"address": { homeaddress:testtt, "officeaddress":testzzzz}
}
如何使用Gson.tojson方法准备JSON文件。
答案 0 :(得分:5)
问题中的示例JSON无效。
我会假设以下格式的JSON是目标。
{
"person":[{"name":"test","id":1}],
"address":{"homeaddress":"testtt","officeaddress":"testzzzz"}
}
使用Gson,您最好使用与JSON完全匹配的Java数据结构。这是一个例子。
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
public class Foo
{
public static void main(String[] args)
{
Person person = new Person("name1", 1);
List<Person> personList = new ArrayList<Person>(1);
personList.add(person);
Address address = new Address("home1", "office1");
Thing thing = new Thing(personList, address);
Gson gson = new Gson();
String json = gson.toJson(thing);
System.out.println(json);
}
}
class Thing
{
private List<Person> person;
private Address address;
Thing(List<Person> person, Address address)
{
this.person = person;
this.address = address;
}
}
class Person
{
private String name;
private int id;
Person(String name, int id)
{
this.name = name;
this.id = id;
}
}
class Address
{
private String homeaddress;
private String officeaddress;
Address(String homeaddress, String officeaddress)
{
this.homeaddress = homeaddress;
this.officeaddress = officeaddress;
}
}
如果不可能拥有与目标JSON结构匹配的Java类结构,并且您只使用原始类,那么您可以使用JsonWriter
一次构建一个JSON一个标记。这是一个例子。
public static void main(String[] args) throws Exception
{
Person person = new Person("name1", 1);
Address address = new Address("home1", "office1");
StringWriter out = new StringWriter();
JsonWriter writer = new JsonWriter(out);
writer.setIndent(" ");
writer.beginObject();
writer.name("person");
writer.beginArray().beginObject();
writer.name("name").value(person.getName());
writer.name("id").value(person.getId());
writer.endObject().endArray();
writer.name("address");
writer.beginObject();
writer.name("homeaddress").value(address.getHomeaddress());
writer.name("officeaddress").value(address.getOfficeaddress());
writer.endObject();
writer.endObject();
writer.close();
System.out.println(out);
}
为了完整起见,我还要指出可以使用一个简单的自定义序列化器来解决这个问题。
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class Foo
{
public static void main(String[] args) throws Exception
{
Person person = new Person("name1", 1);
Address address = new Address("home1", "office1");
SomeContainer thing = new SomeContainer(person, address);
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Person.class, new MyCustomSerializer());
Gson gson = gsonBuilder.create();
System.out.println(gson.toJson(thing));
}
}
class MyCustomSerializer implements JsonSerializer<Person>
{
@Override
public JsonElement serialize(Person src, Type typeOfSrc, JsonSerializationContext context)
{
List<Person> personList = new ArrayList<Person>();
personList.add(src);
return new Gson().toJsonTree(src);
}
}
class SomeContainer
{
Person person;
Address address;
SomeContainer(Person p, Address a) {person = p; address = a;}
}
class Person
{
private String name;
private int id;
Person(String n, int i) {name = n; id = i;}
}
class Address
{
private String homeaddress;
private String officeaddress;
Address(String h, String o) {homeaddress = h; officeaddress = o;}
}
当然,可以从原始Java数据结构构造一个JsonElement
树,然后操纵JSON树以匹配所需的输出,最后将其序列化。