private Object parse(String s) { try { JSONParser parser = new JSONParser(); return parser.parse(s); } catch (Exception e) { throw new IllegalStateException("Invalid JSON: " + s, e); } }
JSONParser parser = new JSONParser(); Object obj = parser.parse(new FileReader("simple.json")); JSONObject jsonObject = (JSONObject) obj; for(Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext();) { String key = (String) iterator.next(); System.out.println(jsonObject.get(key)); }
public static String getJsonWithUpdatedResources(String jsonConf, Map<String, Double> resourceUpdates) { try { JSONParser parser = new JSONParser(); Object obj = parser.parse(jsonConf); JSONObject jsonObject = (JSONObject) obj; Map<String, Double> componentResourceMap = (Map<String, Double>) jsonObject.getOrDefault( Config.TOPOLOGY_COMPONENT_RESOURCES_MAP, new HashMap<String, Double>() ); for (Map.Entry<String, Double> resourceUpdateEntry : resourceUpdates.entrySet()) { if (NormalizedResources.RESOURCE_NAME_NORMALIZER.getResourceNameMapping().containsValue(resourceUpdateEntry.getKey())) { // if there will be legacy values they will be in the outer conf jsonObject.remove(getCorrespondingLegacyResourceName(resourceUpdateEntry.getKey())); componentResourceMap.remove(getCorrespondingLegacyResourceName(resourceUpdateEntry.getKey())); } componentResourceMap.put(resourceUpdateEntry.getKey(), resourceUpdateEntry.getValue()); } jsonObject.put(Config.TOPOLOGY_COMPONENT_RESOURCES_MAP, componentResourceMap); return jsonObject.toJSONString(); } catch (ParseException ex) { throw new RuntimeException("Failed to parse component resources with json: " + jsonConf); } }
@Override public boolean nextKeyValue() throws IOException, InterruptedException { boolean next = lineRecordReader.nextKeyValue(); if (next) { String line = lineRecordReader.getCurrentValue().toString(); try { JSONObject json = (JSONObject) parser.parse(line); String author = (String) json.get("author"); String link = (String) json.get("link"); } catch (ParseException e) { if (skipBadLines) { System.err.println("Bad line at offset: " + lineRecordReader.getCurrentKey().get() + ":\n" + e.getMessage()); badLineCount++; } else { throw new IOException(e); } } } return next; }
private void setExtraEnvParams(String extraEnvParams) { try { if (null != extraEnvParams && extraEnvParams.length() > 0) { JSONParser parser = new JSONParser(); Object obj = parser.parse(extraEnvParams); JSONObject jsonObj = (JSONObject) obj; if (jsonObj.size() > 0) { for (Iterator iterator = jsonObj.keySet().iterator(); iterator.hasNext(); ) { String key = (String) iterator.next(); String val = (String) jsonObj.get(key); if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(val)) { System.setProperty(key.trim(), val.trim()); } } } } } catch (Exception e) { System.out.println( "Failed to parse extra env params: " + extraEnvParams + ". However, ignoring the exception."); e.printStackTrace(); } } }
@Override protected void map(LongWritable offset, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); try { Object o = parser.parse(line); } catch (ParseException e) { if (skipBadLines) { System.err.println("Bad line at offset: " + offset.get() + ":\n" + e.getMessage()); badLineCount.increment(1); return; } else { throw new IOException(e); } } } }
public static Update fromString(String json) { try { // Read json string JSONObject obj = (JSONObject) new JSONParser().parse(json); int buildNumber = ((Long) obj.get("buildNumber")).intValue(); URL download = new URL((String) obj.get("download")); // Return cached update from json return new Update(buildNumber, download, true); } catch (IOException | ParseException ex) { Log.e(TAG, "Invalid JSON object: " + json); } // Shouldn't be returned, but it may happen return new Update(0, null, true); } }
JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(obj); JSONArray lang = (JSONArray) jsonObject.get("Headlines"); JSONArray tag = lang.getJSONArray("tag"); for (int i = 0; i < tag .length(); i++) { JSONObject jsonobject = jsonarray.getJSONObject(i); JSONArray temp= jsonobject .getJSONArray("TagValues"); String tagValue = temp.getString(0); String tagType = jsonobject.getString("TagType"); }
JSONObject data; try { JSONParser helper = new JSONParser(); data = (JSONObject)helper.parse(String); } catch (ParseException parse) { // Invalid syntax return; } // Note that these may throw several exceptions JSONObject node = (JSONObject)data.get("verb"); JSONArray array = (JSONArray)node.get("syn");
JSONObject json; try { json = (JSONObject) PARSER.parse(response); // TODO gson here } catch (ParseException e) { ConsoleMessages.Warn.Crypt.AUTH_FAILED.log(session.getVerifyUsername()); String name = (String) json.get("name"); // NON-NLS String id = (String) json.get("id"); // NON-NLS JSONArray propsArray = (JSONArray) json.get("properties"); // NON-NLS
public static void remove(String dflowname) throws IOException, ParseException { String jsonResponse = ""; URL url = new URL(deleteuri); HttpURLConnection connection = null; connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); // We have to override the post method so we can send data connection.setRequestProperty("X-HTTP-Method-Override", "DELETE"); connection.setDoOutput(true); // Send request OutputStreamWriter wr = new OutputStreamWriter( connection.getOutputStream()); wr.write(dflowname); wr.flush(); // Get Response BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = rd.readLine()) != null) { jsonResponse = jsonResponse.concat(line); } wr.close(); rd.close(); JSONParser parser = new JSONParser(); JSONObject obj = (JSONObject) parser.parse(jsonResponse); System.out.println(obj.get("status").toString()); }
value = Bytes.toStringBinary(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); JSONObject json = (JSONObject) parser.parse(value); String author = (String) json.get("author"); if (context.getConfiguration().get("conf.debug") != null) System.out.println("Author: " + author); context.write(new Text(author), ONE); context.getCounter(Counters.VALID).increment(1); System.err.println("Row: " + Bytes.toStringBinary(row.get()) + ", JSON: " + value); context.getCounter(Counters.ERROR).increment(1);
private void formatResponse(String result) { JSONParser parser = new JSONParser(); Object status; try { status = parser.parse(result); } catch (ParseException e) { System.err.println("Invalid response received from AM"); if (opts.verbose) { System.out.println(result); System.out.println(e.getMessage()); } return; } JSONObject root = (JSONObject) status; showMetric("AM State", root, "state"); showMetric("Target Drillbit Count", root.get("summary"), "targetBitCount"); showMetric("Live Drillbit Count", root.get("summary"), "liveBitCount"); showMetric("Unmanaged Drillbit Count", root.get("summary"), "unmanagedCount"); showMetric("Blacklisted Node Count", root.get("summary"), "blackListCount"); showMetric("Free Node Count", root.get("summary"), "freeNodeCount"); }
public static void runGitHubJsonVMTest(String json) throws ParseException { Assume.assumeFalse("Online test is not available", json.isEmpty()); JSONParser parser = new JSONParser(); JSONObject testSuiteObj = (JSONObject) parser.parse(json); TestSuite testSuite = new TestSuite(testSuiteObj); Iterator<TestCase> testIterator = testSuite.iterator(); while (testIterator.hasNext()) { TestCase testCase = testIterator.next(); TestRunner runner = new TestRunner(); List<String> result = runner.runTestCase(testCase); try { Assert.assertTrue(result.isEmpty()); } catch (AssertionError e) { System.out.println(String.format("Error on running testcase %s : %s", testCase.getName(), result.get(0))); throw e; } } }
static Map<String, Double> parseResources(String input) { Map<String, Double> topologyResources = new HashMap<>(); JSONParser parser = new JSONParser(); LOG.debug("Input to parseResources {}", input); try { if (input != null) { Object obj = parser.parse(input); JSONObject jsonObject = (JSONObject) obj; if (jsonObject.containsKey(Config.TOPOLOGY_COMPONENT_RESOURCES_ONHEAP_MEMORY_MB)) { Double topoMemOnHeap = ObjectReader .getDouble(jsonObject.get(Config.TOPOLOGY_COMPONENT_RESOURCES_ONHEAP_MEMORY_MB), null); topologyResources.put(Config.TOPOLOGY_COMPONENT_RESOURCES_ONHEAP_MEMORY_MB, topoMemOnHeap); .getDouble(jsonObject.get(Config.TOPOLOGY_COMPONENT_RESOURCES_OFFHEAP_MEMORY_MB), null); topologyResources.put(Config.TOPOLOGY_COMPONENT_RESOURCES_OFFHEAP_MEMORY_MB, topoMemOffHeap); Double topoCpu = ObjectReader.getDouble(jsonObject.get(Config.TOPOLOGY_COMPONENT_CPU_PCORE_PERCENT), null); topologyResources.put(Config.TOPOLOGY_COMPONENT_CPU_PCORE_PERCENT, topoCpu);
JSONParser parser = new JSONParser(); JSONObject o = (JSONObject) parser.parse(yourJsonAsString); JSONArray ins = (JSONArray) o.get("in"); JSONArray outs = (JSONArray) o.get("out"); String firstIpAddress = ((JSONObject) ins.get(0)).get("ip").toString();
JSONParser parser = new JSONParser(); JSONObject json = (JSONObject) parser.parse(stringToParse);
JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(reader); // get a String from the JSON object String firstName = (String) jsonObject.get("firstname"); System.out.println("The first name is: " + firstName);