Skip to content

Instantly share code, notes, and snippets.

@svendahlstrand
Last active June 9, 2016 14:28
Show Gist options
  • Save svendahlstrand/1393471 to your computer and use it in GitHub Desktop.
Save svendahlstrand/1393471 to your computer and use it in GitHub Desktop.
Shortest way to get JSON from a SiteVision Script portlet
importPackage(java.net);
importPackage(Packages.org.apache.commons.io);
var url = new URL('http://search.twitter.com/search.json?q=sitevision');
var connection = url.openConnection();
var body = IOUtils.toString(connection.getInputStream(), 'UTF-8');
var json = eval('(' + body + ')');
// Print the latest tweet
out.println(json['results'][0]['text']);
@hampusn
Copy link

hampusn commented Jun 9, 2016

Konverterat till Nashorn :)

var javaImports = new JavaImporter(java.net, Packages.org.apache.commons.io);

with (javaImports) {
   var url = new URL('http://api.scb.se/OV0104/v1/doris/sv/ssd/');
   var connection = url.openConnection();
   var body = IOUtils.toString(connection.getInputStream(), 'UTF-8');
   var json = JSON.parse(body);

   // Print the text from the first array item.
    out.println(json[0].text);
} 

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