簡體   English   中英

GSON - 將字符串轉換為 JsonArray

[英]GSON - convert a string into a JsonArray

我正在嘗試將字符串轉換為 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\":\"\"}}]"
JsonArray data = (JsonArray) JsonParser.parseString(str);

toJson()將 json 對象呈現為(json 的)字符串。

您需要fromJson()方法,它將字符串轉換為 json 對象。

嘗試:

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

Gson().fromJson(string, JsonArray::class.java)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM