Skip to content

Instantly share code, notes, and snippets.

@codenameone
Created March 3, 2016 11:29
Show Gist options
  • Save codenameone/808f436c34444c8143b3 to your computer and use it in GitHub Desktop.
Save codenameone/808f436c34444c8143b3 to your computer and use it in GitHub Desktop.
A sample that parses JSON from the ASOIF webservice
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();
@codenameone
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment