@Test public void testParseJson() throws Exception { Map<String, Object> jsonParams = new HashMap<String, Object>(); jsonParams.put("author", "John B. Smith"); jsonParams.put("year", "2000"); String s = JSONUtils.buildJSON(jsonParams); Map<String, Object> map = JSONUtils.parseJSON(s); assertEquals("John B. Smith", map.get("author")); assertEquals("2000", map.get("year")); }
public static Map<String, Object> parseJSON(String jsonBody) { final Map<String, Object> params = new HashMap<String, Object>(); StringReader reader = new StringReader(jsonBody); JsonReader jsonReader = Json.createReader(reader); JsonStructure structure = jsonReader.read(); if (structure == null || structure instanceof JsonArray) { throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation", jsonBody)); } JsonObject object = (JsonObject) structure; for (Entry<String, JsonValue> entry : object.entrySet()) { String key = entry.getKey(); if (key != null && !key.isEmpty()) { JsonValue jsonValue = entry.getValue(); if (jsonValue != null) { Object value = toJavaObject(jsonValue); params.put(key, value); } } } jsonReader.close(); return params; }
JSONUtils { public static Map<String, Object> parseJSON(String jsonBody) { final Map<String, Object> params = new HashMap<String, Object>(); StringReader reader = new StringReader(jsonBody); JsonReader jsonReader = Json.createReader(reader); JsonStructure structure = jsonReader.read(); if (structure == null || structure instanceof JsonArray) { throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation", jsonBody)); } JsonObject object = (JsonObject) structure; for (Entry<String, JsonValue> entry : object.entrySet()) { String key = entry.getKey(); if (key != null && !key.isEmpty()) { JsonValue jsonValue = entry.getValue(); if (jsonValue != null) { Object value = toJavaObject(jsonValue); params.put(key, value); } } } jsonReader.close(); return params; } }
JSONUtils { public static Map<String, Object> parseJSON(String jsonBody) { final Map<String, Object> params = new HashMap<String, Object>(); StringReader reader = new StringReader(jsonBody); JsonReader jsonReader = Json.createReader(reader); JsonStructure structure = jsonReader.read(); if (structure == null || structure instanceof JsonArray) { throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation", jsonBody)); } JsonObject object = (JsonObject) structure; for (Entry<String, JsonValue> entry : object.entrySet()) { String key = entry.getKey(); if (key != null && !key.isEmpty()) { JsonValue jsonValue = entry.getValue(); if (jsonValue != null) { Object value = toJavaObject(jsonValue); params.put(key, value); } } } jsonReader.close(); return params; }  }
JSONUtils { public static Map<String, Object> parseJSON(String jsonBody) { final Map<String, Object> params = new HashMap<String, Object>(); StringReader reader = new StringReader(jsonBody); JsonReader jsonReader = Json.createReader(reader); JsonStructure structure = jsonReader.read(); if (structure == null || structure instanceof JsonArray) { throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation", jsonBody)); } JsonObject object = (JsonObject) structure; for (Entry<String, JsonValue> entry : object.entrySet()) { String key = entry.getKey(); if (key != null && !key.isEmpty()) { JsonValue jsonValue = entry.getValue(); if (jsonValue != null) { Object value = toJavaObject(jsonValue); params.put(key, value); } } } jsonReader.close(); return params; }  static String buildJSON(Map<String, Object> params); static Map<String, Object> parseJSON(String jsonBody); }
JSONUtils { public static Map<String, Object> parseJSON(String jsonBody) { final Map<String, Object> params = new HashMap<String, Object>(); StringReader reader = new StringReader(jsonBody); JsonReader jsonReader = Json.createReader(reader); JsonStructure structure = jsonReader.read(); if (structure == null || structure instanceof JsonArray) { throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation", jsonBody)); } JsonObject object = (JsonObject) structure; for (Entry<String, JsonValue> entry : object.entrySet()) { String key = entry.getKey(); if (key != null && !key.isEmpty()) { JsonValue jsonValue = entry.getValue(); if (jsonValue != null) { Object value = toJavaObject(jsonValue); params.put(key, value); } } } jsonReader.close(); return params; }  static String buildJSON(Map<String, Object> params); static Map<String, Object> parseJSON(String jsonBody);  }