你需要这个:
package stackoverflow.questions.q18943680;
import java.util.ArrayList;
public class Owner {
String username;
String password;
String email;
String name;
String _id;
int __v;
ArrayList<String> phones;
@Override
public String toString() {
return "Owner [username=" + username + ", password=" + password
+ ", email=" + email + ", name=" + name + ", _id=" + _id
+ ", __v=" + __v + ", phones=" + phones + "]";
}
}
这:
package stackoverflow.questions.q18943680;
public class Facility {
String url;
@Override
public String toString() {
return "Facility [url=" + url + "]";
}
}
还有这个:
package stackoverflow.questions.q18943680;
import java.util.ArrayList;
public class TestBean {
public Owner owner;
public ArrayList<Facility> facilities;
@Override
public String toString() {
return "TestBean [owner=" + owner + ", facilities=" + facilities + "]";
}
}
你会得到你的结果:
package stackoverflow.questions.q18943680;
import com.google.gson.Gson;
public class Q18943680 {
/**
* @param args
*/
public static void main(String[] args) {
String json = "{"+
" \"owner\": { "+
" \"username\": \"bobby\", "+
" \"password\": \"$2a$10$KTouvpAZwyUiCOsvMej.i.zNcGVt94d.n64MHWyxX35ZR03tWCIui\", "+
" \"email\": \"bobby@email.com\", "+
" \"name\": \"Bobby P\", "+
" \"_id\": \"51a59d917997582c3a000003\", "+
" \"__v\": 0, "+
" "+
" \"phones\": [ "+
" \"11118012888\", "+
" \"12341234\", "+
" \"987655775\" "+
" "+
" ], "+
" \"altemails\": [ "+
" \"a@a.com\", "+
" \"b@b.com\", "+
" \"c@c.com\" "+
" ] "+
" "+
"}, "+
" "+
"\"facilities\": [ "+
" { "+
" \"url\": \"/v1/facilities/51a5a6237997582c3a000006\" "+
" }, "+
" { "+
" \"url\": \"/v1/facilities/51a5a6237997582c3a000007\" "+
" }, "+
" { "+
" \"url\": \"/v1/facilities/51a5b533ebcefa893d000003\" "+
" }, "+
" { "+
" \"url\": \"/v1/facilities/51b0238edadae8024f000003\" "+
" } "+
"]} ";
Gson g = new Gson();
TestBean tb = g.fromJson(json, TestBean.class);
System.out.println("This is the result: "+tb.facilities);
}
}
这给了你:
This is the result: [Facility [url=/v1/facilities/51a5a6237997582c3a000006], Facility [url=/v1/facilities/51a5a6237997582c3a000007], Facility [url=/v1/facilities/51a5b533ebcefa893d000003], Facility [url=/v1/facilities/51b0238edadae8024f000003]]
我建议您使用http://json.parser.online.fr/快速解析您的 JSON 以获得关于如何将事物安排到您的 JSON 中以构建其他 bean 的可视化描述。