Created
March 3, 2016 11:29
-
-
Save codenameone/808f436c34444c8143b3 to your computer and use it in GitHub Desktop.
A sample that parses JSON from the ASOIF webservice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Form hi = new Form("JSON Parsing", new BoxLayout(BoxLayout.Y_AXIS)); | |
JSONParser json = new JSONParser(); | |
try(Reader r = new InputStreamReader(Display.getInstance().getResourceAsStream(getClass(), "/anapioficeandfire.json"), "UTF-8")) { | |
Map<String, Object> data = json.parseJSON(r); | |
java.util.List<Map<String, Object>> content = (java.util.List<Map<String, Object>>)data.get("root"); // <1> | |
for(Map<String, Object> obj : content) { // <2> | |
String url = (String)obj.get("url"); | |
String name = (String)obj.get("name"); | |
java.util.List<String> titles = (java.util.List<String>)obj.get("titles"); // <3> | |
if(name == null || name.length() == 0) { | |
java.util.List<String> aliases = (java.util.List<String>)obj.get("aliases"); | |
if(aliases != null && aliases.size() > 0) { | |
name = aliases.get(0); | |
} | |
} | |
MultiButton mb = new MultiButton(name); | |
if(titles != null && titles.size() > 0) { | |
mb.setTextLine2(titles.get(0)); | |
} | |
mb.addActionListener((e) -> Display.getInstance().execute(url)); | |
hi.add(mb); | |
} | |
} catch(IOException err) { | |
Log.e(err); | |
} | |
hi.show(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Parses the data from here: https://gist.github.com/codenameone/c00e2e09aa03cc24e1d3
This is originally from https://anapioficeandfire.com/
Sample usage of JSONParser.
From the Codename One project