简体   繁体   中英

JSON parsing only one branch of whole message java using GsonBuilder and InputStream Android

I just try to integrate with external webservice via JSON from Android. I receive following JSON format:

在此处输入图片说明

Data that i'm interested in is in "messages" branch. To access data i'm using :

builder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY);
Gson gson = builder.create();
ClassToStore response = gson.fromJson(reader, ClassToStore.class);

where reader is a input stream from:

am = getInstrumentation().getContext().getAssets();
am.open("data.json");

Message structure looks like:

在此处输入图片说明

ClassToStore has all fields with the same names.

I get all objects but all of theme are null's

PLEASE HELP :(

My classToStore:

public static class ClassToStore implements Serializable { private static final long serialVersionUID = -1463052486583654136L;

public String id ;
public String replied_to_id ;
public String sender_id ;
public String created_at ;

public String getId() {
    return id;
}

public String getReplied_to_id() {
    return replied_to_id;
}

public String getSender_id() {
    return sender_id;
}

public String getCreated_at() {
    return created_at;
}

}

You will need an extra class to match the outer object:

public class OuterObject {
    List<ClassToStore> messages;    
}

And then load it like this:

Gson gson = new Gson();
Type type = new TypeToken<List<OuterObject>>(){}.getType();
List<OuterObject> outerList = gson.fromJson(reader, type);
List<ClassToStore> listOfMessages = outerlist.get(0).messages;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM