我正在尝试从challonge.com的JSON文件中获取特定值(http://api.challonge.com/v1/documents/participants/index上的更多信息)。我想要获得的值是“id”和“name”。
这是我目前的代码。
HttpClient client = HttpClients.createDefault();
HttpGet request = new HttpGet(jsonUrl);
try {
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if(entity != null){
//Allows me to search through the Participant Object using the
//Participant/Participants POJO
Gson gson = new GsonBuilder().
registerTypeAdapter(Participant.class, new MyDeserializer<>()).
create();
ContentType contentType = ContentType.getOrDefault(entity);
Charset charset = contentType.getCharset();
Reader reader = new InputStreamReader(entity.getContent(), charset);
/*Goes through seperate JSON files received from the Challonge API.
*/
ArrayList<Participant> al = new ArrayList<>();
Type type = new TypeToken<List<Participant>>(){}.getType();
ArrayList<Participant> list = gson.fromJson(reader, al.getClass());
我也使用了type变量而不是al.getClass(),但每当我决定检查id和名称时,两者都给出了java.lang.NullPointerException的错误:
System.out.println(list.get(0).getPlayerId() + ": " + list.get(0).getPlayerName());
此处还有我的参与者课程
public class Participant {
private String id;
private String name;
public String getPlayerId(){
return id;
}
public String getPlayerName(){
return name;
}
}
这是一些json代码的例子
[
{
"participant": {
"active": true,
"checked_in_at": null,
"created_at": "2015-01-19T16:54:40-05:00",
"final_rank": null,
"group_id": null,
"icon": null,
"id": 16543993,
"invitation_id": null,
"invite_email": null,
"misc": null,
"name": "Participant #1",
"on_waiting_list": false,
"seed": 1,
"tournament_id": 1086875,
"updated_at": "2015-01-19T16:54:40-05:00",
"challonge_username": null,
"challonge_email_address_verified": null,
"removable": true,
"participatable_or_invitation_attached": false,
"confirm_remove": true,
"invitation_pending": false,
"display_name_with_invitation_email_address": "Participant #1",
"email_hash": null,
"username": null,
"attached_participatable_portrait_url": null,
"can_check_in": false,
"checked_in": false,
"reactivatable": false
}
},
{
"participant": {
"active": true,
"checked_in_at": null,
"created_at": "2015-01-19T16:54:43-05:00",
"final_rank": null,
"group_id": null,
"icon": null,
"id": 16543994,
"invitation_id": null,
"invite_email": null,
"misc": null,
"name": "Participant #2",
"on_waiting_list": false,
"seed": 2,
"tournament_id": 1086875,
"updated_at": "2015-01-19T16:54:43-05:00",
"challonge_username": null,
"challonge_email_address_verified": null,
"removable": true,
"participatable_or_invitation_attached": false,
"confirm_remove": true,
"invitation_pending": false,
"display_name_with_invitation_email_address": "Participant #2",
"email_hash": null,
"username": null,
"attached_participatable_portrait_url": null,
"can_check_in": false,
"checked_in": false,
"reactivatable": false
}
}
]
提前感谢您的帮助!
答案 0 :(得分:0)
不幸的是,您的JSON比Java对象层次结构深一级。它不包含参与者列表,而是每个包含参与者的事物列表。
试试这个:
public class ParticipantWrapper {
public final Participant participant;
public ParticipantWrapper {
participant = null;
}
}
然后:
ArrayList<ParticipantWrapper> list = gson.fromJson(reader, new TypeToken<List<ParticipantWrapper>>(){}.getType());
最后:
final String id = list.get(0).participant.getPlayerId();
编辑:请查看此完整且有效的示例:https://gist.github.com/DavidMihola/2ee7740a2c0c22b016b4
答案 1 :(得分:0)
我实际上最终找到了适合我的方法。
HttpClient client = HttpClients.createDefault();
HttpGet request = new HttpGet(jsonUrl);
try {
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if(entity != null){
//Allows me to search through the Participant Object using the
//Participant/Participants POJO
Gson gson = new GsonBuilder().
registerTypeAdapter(ParticipantWrapper.class, new MyDeserializer<>()).
create();
ContentType contentType = ContentType.getOrDefault(entity);
Charset charset = contentType.getCharset();
Reader reader = new InputStreamReader(entity.getContent(), charset);
/*Goes through seperate JSON files received from the Challonge API.
*/
ArrayList<Participant> al = new ArrayList<>();
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(reader);
if(jsonElement.isJsonArray()){
Iterator itor = jsonElement.getAsJsonArray().iterator();
while(itor.hasNext()){
JsonObject jObject = (JsonObject) itor.next();
al.add(gson.fromJson(jObject.get("participant"), Participant.class));
}
}
return al;
}
} catch (IOException ex) {
Logger.getLogger(TournamentInfo.class.getName()).log(Level.SEVERE, null, ex);
return null;
}