gpt4 book ai didi

java - 使用 minimal-json 和 Gson 进行 JSON 解析

转载 作者:行者123 更新时间:2023-11-30 11:26:26 27 4
gpt4 key购买 nike

我有一个 java 程序可以检索特定电视节目的数据并返回包含详细信息的 JSON 响应。我的问题是解析 JSON 响应以获得一个特定的键值对,即“first_aired_iso”字符串,它给出了该剧集的播出日期。

这是调用API的URL

String api_url = "http://api.trakt.tv/show/season.json/<api-key>/the-walking-dead/1";

这是相应的 JSON 响应


! [
{
"season":1,
"episode":1,
"number":1,
"tvdb_id":2493011,
"title":"Days Gone Bye",
"overview":"Rick searches for his family after emerging from a coma into a world terrorized by the walking dead. Morgan and Duane, whom he meets along the way, help teach Rick the new rules for survival.",
"first_aired":1288584000,
"first_aired_iso":"2010-10-31T21:00:00-05:00",
"first_aired_utc":1288598400,
"url":"http://trakt.tv/show/the-walking-dead/season/1/episode/1",
"screen":"http://slurm.trakt.us/images/episodes/124-1-1.22.jpg",
"images":{
"screen":"http://slurm.trakt.us/images/episodes/124-1-1.22.jpg"
},
"ratings":{
"percentage":89,
"votes":763,
"loved":733,
"hated":30
},
"watched":true,
"in_collection":true,
"in_watchlist":false,
"rating":false,
"rating_advanced":0
},
{
"season":1,
"episode":2,
"number":2,
"tvdb_id":2493181,
"title":"Guts",
"overview":"Rick unknowingly causes a group of survivors to be trapped by walkers. The group dynamic devolves from accusations to violence, as Rick must confront an enemy far more dangerous than the undead.",
"first_aired":1289192400,
"first_aired_iso":"2010-11-07T21:00:00-06:00",
"first_aired_utc":1289210400,
"url":"http://trakt.tv/show/the-walking-dead/season/1/episode/2",
"screen":"http://slurm.trakt.us/images/episodes/124-1-2.22.jpg",
"images":{
"screen":"http://slurm.trakt.us/images/episodes/124-1-2.22.jpg"
},
"ratings":{
"percentage":87,
"votes":533,
"loved":507,
"hated":26
},
"watched":true,
"in_collection":true,
"in_watchlist":false,
"rating":false,
"rating_advanced":0
},
{
"season":1,
"episode":3,
"number":3,
"tvdb_id":2656081,
"title":"Tell It to the Frogs",
"overview":"After returning to the camp with the department store survivors and an emotional reunion with his wife and son, Rick decides to go against Shane's advice and go back to Atlanta for Merle Dixon and his dropped bag of guns accompanied by Merle's younger brother, Darryl Dixon, as well as Glenn and T-Dog.",
"first_aired":1289797200,
"first_aired_iso":"2010-11-14T21:00:00-06:00",
"first_aired_utc":1289815200,
"url":"http://trakt.tv/show/the-walking-dead/season/1/episode/3",
"screen":"http://slurm.trakt.us/images/episodes/124-1-3.22.jpg",
"images":{
"screen":"http://slurm.trakt.us/images/episodes/124-1-3.22.jpg"
},
"ratings":{
"percentage":85,
"votes":484,
"loved":458,
"hated":26
},
"watched":true,
"in_collection":true,
"in_watchlist":false,
"rating":false,
"rating_advanced":0
},
{
"season":1,
"episode":4,
"number":4,
"tvdb_id":2656091,
"title":"Vatos",
"overview":"While still in search of Merle, the group tries to retrieve the bag of guns but are attacked by several living men who are also after the weapons. The group manages to grab the injured attacker; however, several of the attackers escape and take Glenn hostage. Back at camp a large group of walkers venture up the hill and take the survivors by surprise.",
"first_aired":1290402000,
"first_aired_iso":"2010-11-21T21:00:00-06:00",
"first_aired_utc":1290420000,
"url":"http://trakt.tv/show/the-walking-dead/season/1/episode/4",
"screen":"http://slurm.trakt.us/images/episodes/124-1-4.22.jpg",
"images":{
"screen":"http://slurm.trakt.us/images/episodes/124-1-4.22.jpg"
},
"ratings":{
"percentage":88,
"votes":490,
"loved":467,
"hated":23
},
"watched":true,
"in_collection":true,
"in_watchlist":false,
"rating":false,
"rating_advanced":0
},
{
"season":1,
"episode":5,
"number":5,
"tvdb_id":2656101,
"title":"Wildfire",
"overview":"Rick leads the group to the CDC after the attack on the camp. Jim must make a terrible life and death decision.",
"first_aired":1291006800,
"first_aired_iso":"2010-11-28T21:00:00-06:00",
"first_aired_utc":1291024800,
"url":"http://trakt.tv/show/the-walking-dead/season/1/episode/5",
"screen":"http://slurm.trakt.us/images/episodes/124-1-5.22.jpg",
"images":{
"screen":"http://slurm.trakt.us/images/episodes/124-1-5.22.jpg"
},
"ratings":{
"percentage":87,
"votes":460,
"loved":443,
"hated":17
},
"watched":true,
"in_collection":true,
"in_watchlist":false,
"rating":"love",
"rating_advanced":10
},
{
"season":1,
"episode":6,
"number":6,
"tvdb_id":2656111,
"title":"TS-19",
"overview":"Rick and the group are allowed into the CDC by a strange doctor, but all is not what it seems in their newfound haven. ",
"first_aired":1291611600,
"first_aired_iso":"2010-12-05T21:00:00-06:00",
"first_aired_utc":1291629600,
"url":"http://trakt.tv/show/the-walking-dead/season/1/episode/6",
"screen":"http://slurm.trakt.us/images/episodes/124-1-6.22.jpg",
"images":{
"screen":"http://slurm.trakt.us/images/episodes/124-1-6.22.jpg"
},
"ratings":{
"percentage":87,
"votes":465,
"loved":440,
"hated":25
},
"watched":true,
"in_collection":true,
"in_watchlist":false,
"rating":"love",
"rating_advanced":10
}
]


嗯,你懂的……我只需要为每一集提取“first_aired_iso”的键值。

这是我的代码

        String s = response.toString();
Gson gson = new Gson();
String jsonstr = gson.toJson(s);
System.out.println(jsonstr);
JsonArray json = JsonArray.readFrom(jsonstr);
for (int i = 0; i < 15; i++) {
System.out.println(json.get(i).asString());
}

问题是,打印原始 JSON 输出后完全没有响应。我根本无法让它进入循环。

引用请查看http://eclipsesource.com/blogs/2013/04/18/minimal-json-parser-for-java/

最佳答案

我的解决方案:

public static void main(String[] args) {
String json = "[ { \"season\":1, \"episode\":1, \"number\":1, \"tvdb_id\":2493011, \"title\":\"Days Gone Bye\", \"overview\":\"Rick searches for his family after emerging from a coma into a world terrorized by the walking dead. Morgan and Duane, whom he meets along the way, help teach Rick the new rules for survival.\", \"first_aired\":1288584000, \"first_aired_iso\":\"2010-10-31T21:00:00-05:00\", \"first_aired_utc\":1288598400, \"url\":\"http://trakt.tv/show/the-walking-dead/season/1/episode/1\", \"screen\":\"http://slurm.trakt.us/images/episodes/124-1-1.22.jpg\", \"images\":{ \"screen\":\"http://slurm.trakt.us/images/episodes/124-1-1.22.jpg\" }, \"ratings\":{ \"percentage\":89, \"votes\":763, \"loved\":733, \"hated\":30 }, \"watched\":true, \"in_collection\":true, \"in_watchlist\":false, \"rating\":false, \"rating_advanced\":0 }, { \"season\":1, \"episode\":2, \"number\":2, \"tvdb_id\":2493181, \"title\":\"Guts\", \"overview\":\"Rick unknowingly causes a group of survivors to be trapped by walkers. The group dynamic devolves from accusations to violence, as Rick must confront an enemy far more dangerous than the undead.\", \"first_aired\":1289192400, \"first_aired_iso\":\"2010-11-07T21:00:00-06:00\", \"first_aired_utc\":1289210400, \"url\":\"http://trakt.tv/show/the-walking-dead/season/1/episode/2\", \"screen\":\"http://slurm.trakt.us/images/episodes/124-1-2.22.jpg\", \"images\":{ \"screen\":\"http://slurm.trakt.us/images/episodes/124-1-2.22.jpg\" }, \"ratings\":{ \"percentage\":87, \"votes\":533, \"loved\":507, \"hated\":26 }, \"watched\":true, \"in_collection\":true, \"in_watchlist\":false, \"rating\":false, \"rating_advanced\":0 }, { \"season\":1, \"episode\":3, \"number\":3, \"tvdb_id\":2656081, \"title\":\"Tell It to the Frogs\", \"overview\":\"After returning to the camp with the department store survivors and an emotional reunion with his wife and son, Rick decides to go against Shane's advice and go back to Atlanta for Merle Dixon and his dropped bag of guns accompanied by Merle's younger brother, Darryl Dixon, as well as Glenn and T-Dog.\", \"first_aired\":1289797200, \"first_aired_iso\":\"2010-11-14T21:00:00-06:00\", \"first_aired_utc\":1289815200, \"url\":\"http://trakt.tv/show/the-walking-dead/season/1/episode/3\", \"screen\":\"http://slurm.trakt.us/images/episodes/124-1-3.22.jpg\", \"images\":{ \"screen\":\"http://slurm.trakt.us/images/episodes/124-1-3.22.jpg\" }, \"ratings\":{ \"percentage\":85, \"votes\":484, \"loved\":458, \"hated\":26 }, \"watched\":true, \"in_collection\":true, \"in_watchlist\":false, \"rating\":false, \"rating_advanced\":0 }, { \"season\":1, \"episode\":4, \"number\":4, \"tvdb_id\":2656091, \"title\":\"Vatos\", \"overview\":\"While still in search of Merle, the group tries to retrieve the bag of guns but are attacked by several living men who are also after the weapons. The group manages to grab the injured attacker; however, several of the attackers escape and take Glenn hostage. Back at camp a large group of walkers venture up the hill and take the survivors by surprise.\", \"first_aired\":1290402000, \"first_aired_iso\":\"2010-11-21T21:00:00-06:00\", \"first_aired_utc\":1290420000, \"url\":\"http://trakt.tv/show/the-walking-dead/season/1/episode/4\", \"screen\":\"http://slurm.trakt.us/images/episodes/124-1-4.22.jpg\", \"images\":{ \"screen\":\"http://slurm.trakt.us/images/episodes/124-1-4.22.jpg\" }, \"ratings\":{ \"percentage\":88, \"votes\":490, \"loved\":467, \"hated\":23 }, \"watched\":true, \"in_collection\":true, \"in_watchlist\":false, \"rating\":false, \"rating_advanced\":0 }, { \"season\":1, \"episode\":5, \"number\":5, \"tvdb_id\":2656101, \"title\":\"Wildfire\", \"overview\":\"Rick leads the group to the CDC after the attack on the camp. Jim must make a terrible life and death decision.\", \"first_aired\":1291006800, \"first_aired_iso\":\"2010-11-28T21:00:00-06:00\", \"first_aired_utc\":1291024800, \"url\":\"http://trakt.tv/show/the-walking-dead/season/1/episode/5\", \"screen\":\"http://slurm.trakt.us/images/episodes/124-1-5.22.jpg\", \"images\":{ \"screen\":\"http://slurm.trakt.us/images/episodes/124-1-5.22.jpg\" }, \"ratings\":{ \"percentage\":87, \"votes\":460, \"loved\":443, \"hated\":17 }, \"watched\":true, \"in_collection\":true, \"in_watchlist\":false, \"rating\":\"love\", \"rating_advanced\":10 }, { \"season\":1, \"episode\":6, \"number\":6, \"tvdb_id\":2656111, \"title\":\"TS-19\", \"overview\":\"Rick and the group are allowed into the CDC by a strange doctor, but all is not what it seems in their newfound haven. \", \"first_aired\":1291611600, \"first_aired_iso\":\"2010-12-05T21:00:00-06:00\", \"first_aired_utc\":1291629600, \"url\":\"http://trakt.tv/show/the-walking-dead/season/1/episode/6\", \"screen\":\"http://slurm.trakt.us/images/episodes/124-1-6.22.jpg\", \"images\":{ \"screen\":\"http://slurm.trakt.us/images/episodes/124-1-6.22.jpg\" }, \"ratings\":{ \"percentage\":87, \"votes\":465, \"loved\":440, \"hated\":25 }, \"watched\":true, \"in_collection\":true, \"in_watchlist\":false, \"rating\":\"love\", \"rating_advanced\":10 } ]";
ArrayList<Map> al = new Gson().fromJson(json, ArrayList.class);
for(Map m : al){
System.out.printf("Ep. %.0f aired on %s\n", m.get("episode"), m.get("first_aired_iso"));
}
}

这是执行:

1 aired on 2010-10-31T21:00:00-05:00
2 aired on 2010-11-07T21:00:00-06:00
3 aired on 2010-11-14T21:00:00-06:00
4 aired on 2010-11-21T21:00:00-06:00
5 aired on 2010-11-28T21:00:00-06:00
6 aired on 2010-12-05T21:00:00-06:00

您应该始终让 Gson 为您完成工作。在这种情况下,您有一个对象列表( map )。所以告诉 Gson 以这种方式反序列化并使用通用的 for 循环和映射访问访问结构。

关于java - 使用 minimal-json 和 Gson 进行 JSON 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19821542/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com