List of usage examples for org.json.simple JSONValue parse
public static Object parse(String s)
From source file:importer.handler.post.stages.Splitter.java
/** test and commandline utility */ public static void main(String[] args) { if (args.length >= 1) { try {// w w w. ja va2 s . co m int i = 0; int fileIndex = 0; // see if the user supplied a conf file String textConf = Discriminator.defaultConf; while (i < args.length) { if (args[i].equals("-c") && i < args.length - 1) { textConf = readConfig(args[i + 1]); i += 2; } else { fileIndex = i; i++; } } File f = new File(args[fileIndex]); char[] data = new char[(int) f.length()]; FileReader fr = new FileReader(f); fr.read(data); JSONObject config = (JSONObject) JSONValue.parse(textConf); Splitter split = new Splitter(config); Map<String, String> map = split.split(new String(data)); Set<String> keys = map.keySet(); String rawFileName = args[fileIndex]; int pos = rawFileName.lastIndexOf("."); if (pos != -1) rawFileName = rawFileName.substring(0, pos); Iterator<String> iter = keys.iterator(); while (iter.hasNext()) { String key = iter.next(); String fName = rawFileName + "-" + key + ".xml"; File g = new File(fName); if (g.exists()) g.delete(); FileOutputStream fos = new FileOutputStream(g); fos.write(map.get(key).getBytes("UTF-8")); fos.close(); } } catch (Exception e) { e.printStackTrace(System.out); } } else System.out.println("usage: java -jar split.jar [-c json-config] <tei-xml>\n"); }
From source file:net.jselby.pc.JsonParser.java
public static JSONObject readJsonFromUrl(String url) throws IOException { InputStream is = new URL(url).openStream(); try {//from ww w . ja va2 s . co m BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JSONObject json = (JSONObject) JSONValue.parse(jsonText); return json; } finally { is.close(); } }
From source file:com.mycompany.oauthclienttest.VSOTokenExtractor.java
@Override public Token extract(String string) { Map json = (Map) JSONValue.parse(string); String token = (String) json.get("access_token"); return new Token(token, EMPTY_SECRET); }
From source file:biomine.bmvis2.crawling.Databases.java
public static Collection<String> getDatabases() { if (dbs != null) return dbs; try {//ww w.j a v a 2 s. c o m String url = WebConstants.BIOMINE_URL + "stats/index.cgi?json_action=getdbs"; String cont = URLUtils.getURLContents(new URL(url)); Object arr = JSONValue.parse(cont); if (arr instanceof JSONArray) { JSONArray jarr = (JSONArray) arr; dbs = new ArrayList(); for (Object dbo : jarr) { JSONObject jdb = (JSONObject) dbo; Object no = jdb.get("name"); if (no != null) dbs.add(no.toString()); } Collections.reverse(dbs); return dbs; } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassCastException e) { // TODO: handle exception } return Collections.EMPTY_LIST; }
From source file:com.olabini.jescov.generators.JsonIngester.java
public CoverageData ingest(Reader reader) throws IOException { Map<String, Object> input = (Map<String, Object>) JSONValue.parse(reader); List<FileCoverage> fcs = new ArrayList<FileCoverage>(); for (Map.Entry<String, Object> me : input.entrySet()) { Collection<LineCoverage> lcs = new ArrayList<LineCoverage>(); Collection<BranchCoverage> bcs = new ArrayList<BranchCoverage>(); List<List<Object>> lines = (List<List<Object>>) me.getValue(); for (List<Object> lineInfo : lines) { int lineNumber = ((Long) lineInfo.get(0)).intValue(); int hits = ((Long) lineInfo.get(1)).intValue(); List<List<Object>> branches = (List<List<Object>>) lineInfo.get(2); lcs.add(new LineCoverage(lineNumber, hits)); for (List<Object> bc : branches) { int branchId = ((Long) bc.get(0)).intValue(); List<Long> actualCoverage = (List<Long>) bc.get(1); int[] branchHits = new int[actualCoverage.size()]; int index = 0; for (Long hit : actualCoverage) { branchHits[index++] = hit.intValue(); }//from www . j ava 2 s .com bcs.add(new BranchCoverage(lineNumber, branchId, branchHits)); } } fcs.add(new FileCoverage(me.getKey(), lcs, bcs)); } return new CoverageData(fcs); }
From source file:net.maxgigapop.mrs.driver.openstack.OpenStackRESTClient.java
public static String getToken(String host, String tenant, String username, String password) throws IOException { String url = String.format("http://%s:5000/v2.0/tokens", host); String body = String.format( "{\"auth\": {\"tenantName\": \"%s\", \"passwordCredentials\": {\"username\": \"%s\", \"password\": \"%s\"}}}", tenant, username, password); URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); String responseStr = sendPOST(obj, con, body); JSONObject responseJSON = (JSONObject) JSONValue.parse(responseStr); String tokenId = (String) ((JSONObject) ((JSONObject) responseJSON.get("access")).get("token")).get("id"); return tokenId; }
From source file:fr.free.movierenamer.utils.JSONUtilsTest.java
@BeforeClass public static void init() throws FileNotFoundException, UnsupportedEncodingException, URISyntaxException { URL url = JSONUtilsTest.class.getResource("json.txt"); File file = new File(url.toURI()); Reader reader = new FileReader(file); json = (JSONObject) JSONValue.parse(reader); }
From source file:com.punyal.blackhole.utils.Parsers.java
public static JSONObject senml2json(String s) { JSONObject json, tmp;/*from w w w. j a va 2 s . co m*/ String data; json = new JSONObject(); try { tmp = (JSONObject) JSONValue.parse(s); // Save base time json.put("time", tmp.get("bt")); JSONArray slideContent = (JSONArray) tmp.get("e"); Iterator i = slideContent.iterator(); while (i.hasNext()) { JSONObject slide = (JSONObject) i.next(); json.put(slide.get("n"), slide.get("v")); } } catch (NullPointerException e) { } return json; }
From source file:capstone.yelpmodel.JSONWrapper.java
public JSONWrapper(String src) { json = (JSONArray) JSONValue.parse(src); }
From source file:com.speedment.examples.social.JSONUser.java
public static List<JSONUser> parse(String json) { final JSONObject container = (JSONObject) JSONValue.parse(json); final JSONArray array = (JSONArray) container.get("users"); final List<JSONUser> users = new ArrayList<>(); array.stream().forEach(u -> {//from w w w . ja v a 2 s.c o m users.add(parse((JSONObject) u)); }); return users; }