Lar*_*iro 16 android json gson
如何使用Gson解析此JSON?我有一个包含多个对象类型的数组,我不知道为了保存这个结构我需要创建什么样的对象.我无法更改json消息(我不控制服务器).
功能(类)的唯一类是这个
public class Response {
private List<Object> tr;
private int results;
(...)
}
Run Code Online (Sandbox Code Playgroud)
JSON消息(注意具有多个对象类型的数组.)
{
"tr":
[
{
"a":
{
"userId": "112"
}
},
{
"b":
{
"userId": "123",
"address":"street dummy"
}
},
{
"a":
{
"userId": "154"
}
}
],
"results":3
}
Run Code Online (Sandbox Code Playgroud)
Bri*_*ach 20
Gson用户指南明确涵盖了这一点:
您有一个对象,其字段tr是包含任意类型的数组.
用户指南解释说您无法直接反序列化此类结构,并建议:
使用Gson的解析器API(低级流解析器或DOM解析器JsonParser)来解析数组元素,然后在每个数组元素上使用Gson.fromJson().这是首选方法.
在你的情况下...它真的取决于该数组中可能的对象.如果他们都将拥有相同的内部对象,你会想要做类似......
List<MyUserPojo> list = new ArrayList<MyUserPojo>();
JsonArray array = parser.parse(json).getAsJsonObject().getAsJsonArray("tr");
for (JsonElement je : array)
{
Set<Map.Entry<String,JsonElement>> set = je.getAsObject().entrySet();
JsonElement je2 = set.iterator().next().getValue();
MyUserPojo mup = new Gson().fromJson(je2, MyUserPojo.class);
list.add(mup);
}
Run Code Online (Sandbox Code Playgroud)
当然,这需要在您的实际对象的自定义反序列化器内部,该对象具有tr和results字段.
class MyPojo
{
List<MyUserPojo> userList;
int results;
}
class MyUserPojo
{
String userId;
String address;
}
class MyDeserializer implements JsonDeserializer<MyPojo>
{
@Override
public MyPojo deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
throws JsonParseException
{
List<MyUserPojo> list = new ArrayList<MyUserPojo>();
JsonArray array = je.getAsJsonObject().getAsJsonArray("tr");
for (JsonElement je2 : array)
{
Set<Map.Entry<String,JsonElement>> set = je2.getAsObject().entrySet();
JsonElement je3 = set.iterator().next().getValue();
MyUserPojo mup = new Gson().fromJson(je3, MyUserPojo.class);
list.add(mup);
}
MyPojo mp = new MyPojo();
mp.tr = list;
mp.results = je.getAsObject().getAsJsonPrimitive("results").getAsInt();
return mp;
}
}
Run Code Online (Sandbox Code Playgroud)
现在你已经完成了 - 你可以使用该反序列化器并创建你的对象:
Gson gson = new GsonBuilder()
.registerTypeAdapter(MyPojo.class, new MyDeserializer())
.build();
MyPojo mp = gson.fromJson(json, MyPojo.class);
Run Code Online (Sandbox Code Playgroud)
如果a,b等是重要的......嗯,你必须明白这一点.但是上面的内容可以帮助您了解处理JSON结构所需的内容.
为了完整起见,唯一的"hacky"方法是,如果这些类型的数量相当有限,并且内部对象在其字段方面也相当有限.您可以创建一个包含所有可能性的POJO:
class MyPojo
{
MySecondPojo a;
MySecondPojo b;
...
MySecondPojo f;
}
class MySecondPojo
{
String userId;
String address;
...
String someOtherField;
}
Run Code Online (Sandbox Code Playgroud)
当Gson反序列化JSON时,它会在您的POJO中设置任何缺少的字段null.您现在tr可以List在POJO中成为这些或其中的一组.再次强调,这实际上是非常hacky和错误的方法,但我想我会解释直接解析该数组所需的内容.
我从每个答案中选取了一些内容,并以此方式进行了操作:
响应对象
public class Response {
private List<Users> tr;
private int results;
(...)
}
Run Code Online (Sandbox Code Playgroud)
普通用户
public class User {
public static final int TYPE_USER_A =0;
public static final int TYPE_USER_B =1;
private String userId;
private int type;
(...)
}
Run Code Online (Sandbox Code Playgroud)
一种
public class a extends User {
private String location;
(...)
}
Run Code Online (Sandbox Code Playgroud)
乙
public class b extends User {
private String adress;
(...)
}
Run Code Online (Sandbox Code Playgroud)
解析方法
private Response buildResponseObject(String response) {
Response tls = new Response();
List<Users> users = new ArrayList<users>();
User u;
try {
JSONObject object = new JSONObject(response);
tls.setResults(object.getInt("results"));
JSONArray array = object.getJSONArray("tr");
for (int i = 0; i < array.length(); i++) {
JSONObject trs = array.getJSONObject(i);
if (trs.has("a")) {
String json = trns.getString("a");
A a = new Gson().fromJson(json,A.class);
a.setType(User.TYPE_USER_A);
users.add(a);
} else if (trs.has("b")) {
String json = trs.getString("b");
B b= new Gson().fromJson(json,B.class);
B.setType(User.TYPE_USER_B);
users.add(b);
}
}
tls.setUsers(users);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tls;
}
Run Code Online (Sandbox Code Playgroud)
这不是我想要的那么优雅,并且将本机JsonObjects与Gson方法混合使用,但是对我有用。