Gson解析Json数组
需求:从steam官网获取英雄数据,即为Json数据,并导入到本地数据库
Json数据是这样的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
{
"result": {
"heroes": [
{
"name": "npc_dota_hero_antimage",
"id": 1,
"localized_name": "Anti-Mage"
},
{
"name": "npc_dota_hero_axe",
"id": 2,
"localized_name": "Axe"
},
{
"name": "npc_dota_hero_bane",
"id": 3,
"localized_name": "Bane"
},
{
"name": "npc_dota_hero_bloodseeker",
"id": 4,
"localized_name": "Bloodseeker"
},
{
"name": "npc_dota_hero_crystal_maiden",
"id": 5,
"localized_name": "Crystal Maiden"
},
{
"name": "npc_dota_hero_drow_ranger",
"id": 6,
"localized_name": "Drow Ranger"
},
{
"name": "npc_dota_hero_earthshaker",
"id": 7,
"localized_name": "Earthshaker"
},
{
"name": "npc_dota_hero_juggernaut",
"id": 8,
"localized_name": "Juggernaut"
},
{
"name": "npc_dota_hero_mirana",
"id": 9,
"localized_name": "Mirana"
},
{
"name": "npc_dota_hero_nevermore",
"id": 11,
"localized_name": "Shadow Fiend"
},
{
"name": "npc_dota_hero_morphling",
"id": 10,
"localized_name": "Morphling"
},
{
"name": "npc_dota_hero_phantom_lancer",
"id": 12,
"localized_name": "Phantom Lancer"
},
{
"name": "npc_dota_hero_puck",
"id": 13,
"localized_name": "Puck"
},
{
"name": "npc_dota_hero_pudge",
"id": 14,
"localized_name": "Pudge"
},
{
"name": "npc_dota_hero_razor",
"id": 15,
"localized_name": "Razor"
},
{
"name": "npc_dota_hero_sand_king",
"id": 16,
"localized_name": "Sand King"
},
{
"name": "npc_dota_hero_storm_spirit",
"id": 17,
"localized_name": "Storm Spirit"
},
{
"name": "npc_dota_hero_sven",
"id": 18,
"localized_name": "Sven"
},
{
"name": "npc_dota_hero_tiny",
"id": 19,
"localized_name": "Tiny"
},
{
"name": "npc_dota_hero_vengefulspirit",
"id": 20,
"localized_name": "Vengeful Spirit"
},
{
"name": "npc_dota_hero_windrunner",
"id": 21,
"localized_name": "Windranger"
},
{
"name": "npc_dota_hero_zuus",
"id": 22,
"localized_name": "Zeus"
},
{
"name": "npc_dota_hero_kunkka",
"id": 23,
"localized_name": "Kunkka"
},
{
"name": "npc_dota_hero_lina",
"id": 25,
"localized_name": "Lina"
},
{
"name": "npc_dota_hero_lich",
"id": 31,
"localized_name": "Lich"
},
{
"name": "npc_dota_hero_lion",
"id": 26,
"localized_name": "Lion"
},
{
"name": "npc_dota_hero_shadow_shaman",
"id": 27,
"localized_name": "Shadow Shaman"
},
{
"name": "npc_dota_hero_slardar",
"id": 28,
"localized_name": "Slardar"
},
{
"name": "npc_dota_hero_tidehunter",
"id": 29,
"localized_name": "Tidehunter"
},
{
"name": "npc_dota_hero_witch_doctor",
"id": 30,
"localized_name": "Witch Doctor"
},
{
"name": "npc_dota_hero_riki",
"id": 32,
"localized_name": "Riki"
},
{
"name": "npc_dota_hero_enigma",
"id": 33,
"localized_name": "Enigma"
},
{
"name": "npc_dota_hero_tinker",
"id": 34,
"localized_name": "Tinker"
},
{
"name": "npc_dota_hero_sniper",
"id": 35,
"localized_name": "Sniper"
},
{
"name": "npc_dota_hero_necrolyte",
"id": 36,
"localized_name": "Necrophos"
},
{
"name": "npc_dota_hero_warlock",
"id": 37,
"localized_name": "Warlock"
},
{
"name": "npc_dota_hero_beastmaster",
"id": 38,
"localized_name": "Beastmaster"
},
{
"name": "npc_dota_hero_queenofpain",
"id": 39,
"localized_name": "Queen of Pain"
},
{
"name": "npc_dota_hero_venomancer",
"id": 40,
"localized_name": "Venomancer"
},
{
"name": "npc_dota_hero_faceless_void",
"id": 41,
"localized_name": "Faceless Void"
},
{
"name": "npc_dota_hero_skeleton_king",
"id": 42,
"localized_name": "Wraith King"
},
{
"name": "npc_dota_hero_death_prophet",
"id": 43,
"localized_name": "Death Prophet"
},
{
"name": "npc_dota_hero_phantom_assassin",
"id": 44,
"localized_name": "Phantom Assassin"
},
{
"name": "npc_dota_hero_pugna",
"id": 45,
"localized_name": "Pugna"
},
{
"name": "npc_dota_hero_templar_assassin",
"id": 46,
"localized_name": "Templar Assassin"
},
{
"name": "npc_dota_hero_viper",
"id": 47,
"localized_name": "Viper"
},
{
"name": "npc_dota_hero_luna",
"id": 48,
"localized_name": "Luna"
},
{
"name": "npc_dota_hero_dragon_knight",
"id": 49,
"localized_name": "Dragon Knight"
},
{
"name": "npc_dota_hero_dazzle",
"id": 50,
"localized_name": "Dazzle"
},
{
"name": "npc_dota_hero_rattletrap",
"id": 51,
"localized_name": "Clockwerk"
},
{
"name": "npc_dota_hero_leshrac",
"id": 52,
"localized_name": "Leshrac"
},
{
"name": "npc_dota_hero_furion",
"id": 53,
"localized_name": "Nature's Prophet"
},
{
"name": "npc_dota_hero_life_stealer",
"id": 54,
"localized_name": "Lifestealer"
},
{
"name": "npc_dota_hero_dark_seer",
"id": 55,
"localized_name": "Dark Seer"
},
{
"name": "npc_dota_hero_clinkz",
"id": 56,
"localized_name": "Clinkz"
},
{
"name": "npc_dota_hero_omniknight",
"id": 57,
"localized_name": "Omniknight"
},
{
"name": "npc_dota_hero_enchantress",
"id": 58,
"localized_name": "Enchantress"
},
{
"name": "npc_dota_hero_huskar",
"id": 59,
"localized_name": "Huskar"
},
{
"name": "npc_dota_hero_night_stalker",
"id": 60,
"localized_name": "Night Stalker"
},
{
"name": "npc_dota_hero_broodmother",
"id": 61,
"localized_name": "Broodmother"
},
{
"name": "npc_dota_hero_bounty_hunter",
"id": 62,
"localized_name": "Bounty Hunter"
},
{
"name": "npc_dota_hero_weaver",
"id": 63,
"localized_name": "Weaver"
},
{
"name": "npc_dota_hero_jakiro",
"id": 64,
"localized_name": "Jakiro"
},
{
"name": "npc_dota_hero_batrider",
"id": 65,
"localized_name": "Batrider"
},
{
"name": "npc_dota_hero_chen",
"id": 66,
"localized_name": "Chen"
},
{
"name": "npc_dota_hero_spectre",
"id": 67,
"localized_name": "Spectre"
},
{
"name": "npc_dota_hero_doom_bringer",
"id": 69,
"localized_name": "Doom"
},
{
"name": "npc_dota_hero_ancient_apparition",
"id": 68,
"localized_name": "Ancient Apparition"
},
{
"name": "npc_dota_hero_ursa",
"id": 70,
"localized_name": "Ursa"
},
{
"name": "npc_dota_hero_spirit_breaker",
"id": 71,
"localized_name": "Spirit Breaker"
},
{
"name": "npc_dota_hero_gyrocopter",
"id": 72,
"localized_name": "Gyrocopter"
},
{
"name": "npc_dota_hero_alchemist",
"id": 73,
"localized_name": "Alchemist"
},
{
"name": "npc_dota_hero_invoker",
"id": 74,
"localized_name": "Invoker"
},
{
"name": "npc_dota_hero_silencer",
"id": 75,
"localized_name": "Silencer"
},
{
"name": "npc_dota_hero_obsidian_destroyer",
"id": 76,
"localized_name": "Outworld Devourer"
},
{
"name": "npc_dota_hero_lycan",
"id": 77,
"localized_name": "Lycan"
},
{
"name": "npc_dota_hero_brewmaster",
"id": 78,
"localized_name": "Brewmaster"
},
{
"name": "npc_dota_hero_shadow_demon",
"id": 79,
"localized_name": "Shadow Demon"
},
{
"name": "npc_dota_hero_lone_druid",
"id": 80,
"localized_name": "Lone Druid"
},
{
"name": "npc_dota_hero_chaos_knight",
"id": 81,
"localized_name": "Chaos Knight"
},
{
"name": "npc_dota_hero_meepo",
"id": 82,
"localized_name": "Meepo"
},
{
"name": "npc_dota_hero_treant",
"id": 83,
"localized_name": "Treant Protector"
},
{
"name": "npc_dota_hero_ogre_magi",
"id": 84,
"localized_name": "Ogre Magi"
},
{
"name": "npc_dota_hero_undying",
"id": 85,
"localized_name": "Undying"
},
{
"name": "npc_dota_hero_rubick",
"id": 86,
"localized_name": "Rubick"
},
{
"name": "npc_dota_hero_disruptor",
"id": 87,
"localized_name": "Disruptor"
},
{
"name": "npc_dota_hero_nyx_assassin",
"id": 88,
"localized_name": "Nyx Assassin"
},
{
"name": "npc_dota_hero_naga_siren",
"id": 89,
"localized_name": "Naga Siren"
},
{
"name": "npc_dota_hero_keeper_of_the_light",
"id": 90,
"localized_name": "Keeper of the Light"
},
{
"name": "npc_dota_hero_wisp",
"id": 91,
"localized_name": "Io"
},
{
"name": "npc_dota_hero_visage",
"id": 92,
"localized_name": "Visage"
},
{
"name": "npc_dota_hero_slark",
"id": 93,
"localized_name": "Slark"
},
{
"name": "npc_dota_hero_medusa",
"id": 94,
"localized_name": "Medusa"
},
{
"name": "npc_dota_hero_troll_warlord",
"id": 95,
"localized_name": "Troll Warlord"
},
{
"name": "npc_dota_hero_centaur",
"id": 96,
"localized_name": "Centaur Warrunner"
},
{
"name": "npc_dota_hero_magnataur",
"id": 97,
"localized_name": "Magnus"
},
{
"name": "npc_dota_hero_shredder",
"id": 98,
"localized_name": "Timbersaw"
},
{
"name": "npc_dota_hero_bristleback",
"id": 99,
"localized_name": "Bristleback"
},
{
"name": "npc_dota_hero_tusk",
"id": 100,
"localized_name": "Tusk"
},
{
"name": "npc_dota_hero_skywrath_mage",
"id": 101,
"localized_name": "Skywrath Mage"
},
{
"name": "npc_dota_hero_abaddon",
"id": 102,
"localized_name": "Abaddon"
},
{
"name": "npc_dota_hero_elder_titan",
"id": 103,
"localized_name": "Elder Titan"
},
{
"name": "npc_dota_hero_legion_commander",
"id": 104,
"localized_name": "Legion Commander"
},
{
"name": "npc_dota_hero_ember_spirit",
"id": 106,
"localized_name": "Ember Spirit"
},
{
"name": "npc_dota_hero_earth_spirit",
"id": 107,
"localized_name": "Earth Spirit"
},
{
"name": "npc_dota_hero_terrorblade",
"id": 109,
"localized_name": "Terrorblade"
},
{
"name": "npc_dota_hero_phoenix",
"id": 110,
"localized_name": "Phoenix"
},
{
"name": "npc_dota_hero_oracle",
"id": 111,
"localized_name": "Oracle"
},
{
"name": "npc_dota_hero_techies",
"id": 105,
"localized_name": "Techies"
},
{
"name": "npc_dota_hero_winter_wyvern",
"id": 112,
"localized_name": "Winter Wyvern"
},
{
"name": "npc_dota_hero_arc_warden",
"id": 113,
"localized_name": "Arc Warden"
},
{
"name": "npc_dota_hero_abyssal_underlord",
"id": 108,
"localized_name": "Underlord"
},
{
"name": "npc_dota_hero_monkey_king",
"id": 114,
"localized_name": "Monkey King"
},
{
"name": "npc_dota_hero_pangolier",
"id": 120,
"localized_name": "Pangolier"
},
{
"name": "npc_dota_hero_dark_willow",
"id": 119,
"localized_name": "Dark Willow"
}
],
"status": 200,
"count": 115
}
}
我们用这个封装方法来解析Json数组
public static <T> List<T> getObjectList(String jsonString,Class<T> cls){ List<T> list = new ArrayList<T>(); try { Gson gson = new Gson(); JsonArray arry = new JsonParser().parse(jsonString).getAsJsonArray(); for (JsonElement jsonElement : arry) { list.add(gson.fromJson(jsonElement, cls)); } } catch (Exception e) { e.printStackTrace(); } return list; }
用这个方法来对URL进行解析和设置
public static String getUrlForHero(String strURL) throws IOException { URL url = new URL(strURL); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); httpConn.setRequestMethod("GET"); httpConn.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader( httpConn.getInputStream())); String line; StringBuffer buffer = new StringBuffer(); while ((line = reader.readLine()) != null) { buffer.append(line); } reader.close(); httpConn.disconnect(); String result = new String(buffer.substring(20,buffer.length()-27));//这里把不用的数据去掉,保留heroes之后的数组,status,count不要。 //JSONArray jsonArray = JSONArray.fromObject(result); return result; }
最后这样调用
private void addHeroFromSteamAPI() throws IOException { String jsonArray = URLUtil.getUrlForHero("https://api.steampowered.com/IEconDOTA2_570/GetHeroes/v1/?key=id&format=json&language=Chinese"); List<Hero> heroes = GsonUtil.getObjectList(jsonArray,Hero.class); for (Hero hero:heroes){ HeroBean heroBean = new HeroBean(); heroBean.setId(hero.getId()); heroBean.setName(hero.getName()); heroBean.setLocalizedName(hero.getLocalized_name()); heroService.addHeroFromSteamAPI(heroBean); } }
作者:Rest探路者
出处:http://www.cnblogs.com/Java-Starter/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意请保留此段声明,请在文章页面明显位置给出原文连接
Github:https://github.com/cjy513203427