GSON - 将字符串转换为JsonArray

时间:2017-07-17 23:18:43

标签: java json gson kotlin

我正在尝试将字符串转换为JsonArray。到目前为止,我已尝试执行以下操作:

Gson().toJson(string)

Gson().toJsonTree(string)

都抛出一个异常,说该参数不是JsonArray。

这是字符串,你可以看到它是一个JsonArray:

"[{\"match\":{\"id\":92757102,\"tournament_id\":3666234,\"state\":\"open\",\"player1_id\":58602461,\"player2_id\":58602459,\"player1_prereq_match_id\":null,\"player2_prereq_match_id\":null,\"player1_is_prereq_match_loser\":false,\"player2_is_prereq_match_loser\":false,\"winner_id\":null,\"loser_id\":null,\"started_at\":\"2017-07-17T19:10:07.588-04:00\",\"created_at\":\"2017-07-17T19:10:07.476-04:00\",\"updated_at\":\"2017-07-17T19:10:07.588-04:00\",\"identifier\":\"A\",\"has_attachment\":false,\"round\":1,\"player1_votes\":null,\"player2_votes\":null,\"group_id\":null,\"attachment_count\":null,\"scheduled_time\":null,\"location\":null,\"underway_at\":null,\"optional\":false,\"rushb_id\":null,\"completed_at\":null,\"suggested_play_order\":1,\"prerequisite_match_ids_csv\":\"\",\"scores_csv\":\"\"}}]"

2 个答案:

答案 0 :(得分:-1)

toJson()将json对象呈现为字符串(json)。

您需要fromJson()方法,该方法将字符串转换为json对象。

尝试:

new Gson().fromJson(string, JsonArray.class)

答案 1 :(得分:-1)

??id??

相关问题