Requirements: Get hero data from the steam website, which is JSON data and import to the local database
The JSON data is like this
{"Result": {"Heroes": [{"Name": "Npc_dota_hero_antimage", "id": 1, "Localized_name": "An Ti-mage "}, {" Name ":" Npc_dota_hero_axe "," id ": 2," Localized_name ":" Axe "}, { "Name": "Npc_dota_hero_bane", "id": 3, "Localized_name": "Bane"}, {"Name": "Npc_do Ta_hero_bloodseeker "," id ": 4," Localized_name ":" Bloodseeker "}, {" Name ":" Npc_dota_hero_ Crystal_maiden "," id ": 5," Localized_name ":" Crystal Maiden "}, {" Name ":" Npc_dota_hero_dr Ow_ranger "," id ": 6," Localized_name ":" Drow Ranger "}, {" Name ":" Npc_dota_hero_earthshake R "," 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 ": One," Localized_name ":" Shadow fiend " }, {"Name": "Npc_dota_hero_morphling", "id": Ten, "Localized_name": "Morphling"}, {"Name": "Npc_dota_hero_phantom_lancer", "id": "localized_name": "Phantom Lancer"}, {"Name": "Npc_dota_hero_puck", "id": "localized_name": "Puck"}, {"Name": "NPC _dota_hero_pudge "," id ": +," localized_name ":" Pudge "}, {" Name ":" Npc_dota_hero_razor ", "id": "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":, "localized_n Ame ":" Sven "}, {"Name": "Npc_dota_hero_tiny", "id": +, "localized_name": "Tiny"}, {"Name": "N Pc_dota_hero_vengefulspirit "," id ":" localized_name ":" Vengeful Spirit "}, {" Name ":" N Pc_dota_hero_windrunner "," id ": +," localized_name ":" Windranger "}, {" Name ":" Npc_dota_h Ero_zuus "," id ":" localized_name ":" Zeus "}, {" Name ":" Npc_dota_hero_kunkka "," ID ": Localized_name": "Kunkka"}, {"Name": "Npc_dota_hero_lina", "id": +, "Lo Calized_name ":" Lina "}, {" Name ":" Npc_dota_hero_lich "," id ": +," localized_name ":" Lich " }, {"Name": "Npc_dota_hero_lion", "id": +, "localized_name": "Lion"}, { "Name": "Npc_dota_hero_shadow_shaman", "id": "localized_name": "Shadow Shaman"}, {" Name ":" Npc_dota_hero_slArdar "," id ":" localized_name ":" Slardar "}, {" Name ":" Npc_dota_hero_tidehunter ", "id": "localized_name": "Tidehunter"}, {"Name": "Npc_dota_hero_witch_doctor", "id" : "Localized_name": "Witch Doctor"}, {"Name": "Npc_dota_hero_riki", "id": 32, "Localized_name": "Riki"}, {"Name": "Npc_dota_hero_enigma", "id":, "Localized_name": " Enigma "}, {" Name ":" Npc_dota_hero_tinker "," id ":" localized_name ":" Tinker "}, {"Name": "Npc_dota_hero_sniper", "id": "localized_name": "Sniper"}, {"Nam E ":" Npc_dota_hero_necrolyte "," id ": $," localized_name ":" Necrophos "}, {" Name ":" Npc_do Ta_hero_warlock "," id ": PNS," Localized_name ":" Warlock "}, {" Name ":" Npc_dota_hero_beastma Ster "," id ": 38, "Localized_name": "Beastmaster"}, {"Name": "Npc_dota_hero_queenofpain", "id": 39, "Localized_name": "Queen of Pain"}, {"Name": "Npc_dota_hero_venomancer", "id": +, "local Ized_name ":" Venomancer "}, {" Name ":" Npc_dota_hero_faceless_void "," id ": $," localized_n Ame ":" Faceless Void "}, {" Name ":" Npc_dota_hero_skeleton_king "," id ": All," localized_name ":" Wraith King "}, {" Name ":" Npc_dota_hero_death_prophet "," id ": $," localized_name ":" D Eath Prophet "}, {" Name ":" Npc_dota_hero_phantom_assassin "," id ": $," localized_name ":" P Hantom Assassin "}, {" Name ":" Npc_dota_hero_pugna "," id ": $," localized_name ":" Pugna " }, {"Name": "Npc_dota_hero_templar_assassin", "id": $, "localized_name": "Templar Assassin" }, { "Name ":" Npc_dota_hero_viper "," id ": +," localized_name ":" Viper "}, {" Name ":" Npc_dota_he Ro_luna "," id ":" localized_name ":" Luna "}, {" Name ":" Npc_dota_hero_dragon_knight ", "id": "localized_name": "Dragon Knight"}, {"Name": "Npc_dota_hero_dazzle", "id": "Localized_name": "Dazzle"}, {"Name": "Npc_dota_hero_rattletrap", "id": 51, " Localized_name ":" Clockwerk "}, {" Name ":" Npc_dota_hero_leshrac "," id ": *," Localized_nam E ":" Leshrac "}, {" Name ":" Npc_dota_hero_furion "," id ": +," localized_name ":" Nature ' s Pr Ophet "}, {" Name ":" Npc_dota_hero_life_stealer "," id ": Si," localized_name ":" Lifestealer " }, {"Name": "Npc_dota_hero_dark_seer", "id": "localized_name": "Dark Seer"}, {"Name": "NPC_dota_hero_clinkz "," id ":" localized_name ":" Clinkz "}, {" Name ":" Npc_dota_hero_omnikn Ight "," id ":" localized_name ":" Omniknight "}, {" Name ":" Npc_dota_hero_enchantress ", "id": "localized_name": "Enchantress"}, {"Name": "Npc_dota_hero_huskar", "id": "Localized_name": "Huskar"}, {"Name": "Npc_dota_hero_night_stalker", "id": 60, "Localized_name": "Night Stalker"}, {"Name": "Npc_dota_hero_broodmother", "id": All, "Loc Alized_name ":" Broodmother "}, {" Name ":" Npc_dota_hero_bounty_hunter "," id ": +," localize D_name ":" Bounty Hunter "}, {" Name ":" Npc_dota_hero_weaver "," id ": +," localized_name ":" Weaver "}, {" Name ":" Npc_dota_hero_jakiro "," id ": +," localized_name ":" Jakiro "}, {"Name": "NPC_dota_hero_batrider "," id ": $," localized_name ":" Batrider "}, {" Name ":" Npc_dota_hero_c Hen "," id ":" localized_name ":" Chen "}, {" Name ":" Npc_dota_hero_spectre "," id ": "Localized_name": "Spectre"}, {"Name": "Npc_dota_hero_doom_bringer", "id": 69, "Localized_name": "Doom"}, {"Name": "Npc_dota_hero_ancient_apparition", "id": Loca Lized_name ":" Ancient Apparition "}, {" Name ":" Npc_dota_hero_ursa "," id ": +," localized_n Ame ":" Ursa "}, {" Name ":" Npc_dota_hero_spirit_breaker "," id ": +," localized_name ":" Spir It Breaker "}, {" Name ":" Npc_dota_hero_gyrocopter "," id ":" localized_name ":" Gyrocopte R "}, {" Name ":" Npc_dota_hero_alchemist "," id ":" localized_name ":" Alchemist "}, {"Name": "NPc_dota_hero_invoker "," id ":" localized_name ":" Invoker "}, {" Name ":" Npc_dota_hero_si Lencer "," id ":" localized_name ":" Silencer "}, {" Name ":" Npc_dota_hero_obsidian_destro Yer "," id ":" localized_name ":" Outworld Devourer "}, {" Name ":" Npc_dota_hero_lycan ", "id": "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": +, "Loca Lized_name ":" Lone Druid "}, {" Name ":" Npc_dota_hero_chaos_knight "," id ": Bayi," localized_n Ame ":" Chaos Knight "}, {" Name ":" Npc_dota_hero_meepo "," id ":" localized_name ":" Meepo " " }, {"Name": "Npc_dota_hero_treant", "id": "localized_name": "Treant Protector"}, {"Name": "Npc_dota_hero_ogre_magi", "id": +, "Localized_name": "Ogre Magi"}, {"Name": "Npc_dota_ Hero_undying "," id ":" localized_name ":" Undying "}, {" Name ":" Npc_dota_hero_rubick ", "id": "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": "*", "local Ized_name ":" Naga Siren "}, {" Name ":" Npc_dota_hero_keeper_of_the_light "," id ": All," local Ized_name ":" Keeper of the Light "}, {" Name ":" Npc_dota_hero_wisp "," id ":", "localized_n Ame ":" Io "},{"Name": "Npc_dota_hero_visage", "id": "localized_name": "Visage"}, {"Name": "Npc_dota_hero_slark", "id": "localized_name": "Slark"}, {"Name": "Npc_dota_hero_medu SA "," id ": 94," Localized_name ":" Medusa "}, {" Name ":" Npc_dota_hero_troll_warlord ", "id": "localized_name": "Troll Warlord"}, {"Name": "Npc_dota_hero_centaur", "id": 9 6, "Localized_name": "Centaur Warrunner"}, {"Name": "Npc_dota_hero_magnataur", "ID": 97, "Localized_name": "Magnus"}, {"Name": "Npc_dota_hero_shredder", "id": 98, "Localiz Ed_name ":" Timbersaw "}, {" Name ":" Npc_dota_hero_bristleback "," id ":," Localized_name ": "Bristleback"}, {"Name": "Npc_dota_hero_tusk", "id": +, "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_em Ber_spirit "," id ": 106," Localized_name ":" Ember Spirit "}, {" Name ":" Npc_dota_hero_earth_ Spirit "," id ": 107," Localized_name ":" Earth Spirit "}, {" Name ":" Npc_dota_hero_terrorblad E "," id ": 109," Localized_name ":" Terrorblade "}, {" Name ":" Npc_dota_hero_phoenix ", "id": "localized_name": "Phoenix"}, {"Name": "Npc_dota_hero_oracle", "id": 111, "Localized_name": "ORacle "}, {" Name ":" Npc_dota_hero_techies "," id ":" localized_name ":" Techies "}, {"Name": "Npc_dota_hero_winter_wyvern", "id": "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": "localized_name": "Monkey King"}, {"NA Me ":" Npc_dota_hero_pangolier "," id ": +," localized_name ":" Pangolier "}, {" Name ":" Npc_ Dota_hero_dark_willow "," id ": 119," Localized_name ":" Dark Willow "}]," status ": $," Count " : 115}}
View Code
We use this encapsulation method to parse the JSON array
Public Static<T> list<t> getobjectlist (String jsonstring,class<t>CLS) {List<T> list =NewArraylist<t>(); Try{Gson Gson=NewGson (); Jsonarray Arry=NewJsonparser (). Parse (jsonstring). Getasjsonarray (); for(jsonelement jsonelement:arry) {List.add (Gson.fromjson (Jsonelement, CLS)); } } Catch(Exception e) {e.printstacktrace (); } returnlist; }
Use this method to parse and set the URL
Public StaticString Geturlforhero (String strurl)throwsioexception {URL url=NewURL (strURL); HttpURLConnection Httpconn=(HttpURLConnection) url.openconnection (); Httpconn.setrequestmethod ("GET"); Httpconn.connect (); BufferedReader Reader=NewBufferedReader (NewInputStreamReader (Httpconn.getinputstream ())); String Line; StringBuffer Buffer=NewStringBuffer (); while(line = Reader.readline ())! =NULL) {buffer.append (line); } reader.close (); Httpconn.disconnect (); String result=NewString (Buffer.substring (20,buffer.length ()-27), or//To remove the unused data here, keep the array after heroes, Status,count do not. //Jsonarray Jsonarray = jsonarray.fromobject (result); returnresult; }
Finally, this calls
Private voidAddherofromsteamapi ()throwsIOException {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=NewHerobean (); Herobean.setid (Hero.getid ()); Herobean.setname (Hero.getname ()); Herobean.setlocalizedname (Hero.getlocalized_name ()); Heroservice.addherofromsteamapi (Herobean); } }
Gson parsing JSON arrays