java从challonge获取特定值。使用gson将com json文件转换为ArrayList
我试图从challonge的JSON文件中获取特定值。com(有关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());
我还使用了类型变量,而不是al.getClass(),但这两个变量都给出了java的错误。lang.NullPointerException每当我决定检查id和名称时:
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
}
}
]
提前感谢您的帮助
# 1 楼答案
不幸的是,JSON比Java对象层次结构更深一层。它不包含参与者的列表,而是包含每个参与者的事物的列表
试试这个:
然后:
最后:
编辑:请看一下这个完整且有效的示例:https://gist.github.com/DavidMihola/2ee7740a2c0c22b016b4