此链接描述了使用 Gson 的 json to java 和 java to json
http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
使用 GSON 的 Json 到 Java
Gson gson = new Gson();
try {
BufferedReader br = new StringReader(<CONTENT>);
//convert the json string back to object
// In your case this is Group object
Object obj = gson.fromJson(br, Object .class);
System.out.println(obj);
} catch (IOException e) {
e.printStackTrace();
}
使用 GSON 的 Java 对象到 json 字符串
Object obj = new Object();
Gson gson = new Gson();
// convert java object to JSON format,
// and returned as JSON formatted string
String json = gson.toJson(obj);
在你的情况下使用这个:
public static void main(final String[] args) {
String Group1 = " [ { \"id\": \"2b3b0db\", \"name\": \"Ivan\" }, { \"id\": \"4f3b0db\", \"name\": \"Lera\" } ] ";
Gson gson = new Gson();
Group[] object = gson.fromJson(new StringReader(Group1), Group[].class);
System.out.println(object);
}
public class Group {
private String id;
private String name;
public String getId() {
return id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public void setId(final String id) {
this.id = id;
}
}