Created
March 2, 2016 20:31
-
-
Save codenameone/f9fdacaac12583cd2eed to your computer and use it in GitHub Desktop.
Sample of using the JSONParser Codename One API to parse and display JSON data in a more traditional way
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
[ | |
{ | |
"url": "http://www.anapioficeandfire.com/api/characters/13", | |
"name": "Chayle", | |
"culture": "", | |
"born": "", | |
"died": "In 299 AC, at Winterfell", | |
"titles": [ | |
"Septon" | |
], | |
"aliases": [], | |
"father": "", | |
"mother": "", | |
"spouse": "", | |
"allegiances": [], | |
"books": [ | |
"http://www.anapioficeandfire.com/api/books/1", | |
"http://www.anapioficeandfire.com/api/books/2", | |
"http://www.anapioficeandfire.com/api/books/3" | |
], | |
"povBooks": [], | |
"tvSeries": [], | |
"playedBy": [] | |
}, | |
{ | |
"url": "http://www.anapioficeandfire.com/api/characters/14", | |
"name": "Gillam", | |
"culture": "", | |
"born": "", | |
"died": "", | |
"titles": [ | |
"Brother" | |
], | |
"aliases": [], | |
"father": "", | |
"mother": "", | |
"spouse": "", | |
"allegiances": [], | |
"books": [ | |
"http://www.anapioficeandfire.com/api/books/5" | |
], | |
"povBooks": [], | |
"tvSeries": [], | |
"playedBy": [] | |
}, | |
{ | |
"url": "http://www.anapioficeandfire.com/api/characters/15", | |
"name": "High Septon", | |
"culture": "", | |
"born": "", | |
"died": "", | |
"titles": [ | |
"High Septon", | |
"His High Holiness", | |
"Father of the Faithful", | |
"Voice of the Seven on Earth" | |
], | |
"aliases": [ | |
"The High Sparrow" | |
], | |
"father": "", | |
"mother": "", | |
"spouse": "", | |
"allegiances": [], | |
"books": [ | |
"http://www.anapioficeandfire.com/api/books/5", | |
"http://www.anapioficeandfire.com/api/books/8" | |
], | |
"povBooks": [], | |
"tvSeries": [ | |
"Season 5" | |
], | |
"playedBy": [ | |
"Jonathan Pryce" | |
] | |
} | |
] |
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"); | |
for(Map<String, Object> obj : content) { | |
String url = (String)obj.get("url"); | |
String name = (String)obj.get("name"); | |
java.util.List<String> titles = (java.util.List<String>)obj.get("titles"); | |
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
Sample code for using the JSONParser API.
From the Codename One project