public void demoJson() { String JSON = "{\"attribute\":\"foo\", \"number\":100.959}"; try { JSONObject obj = (JSONObject)JSON.parse(JSON); Double dbl = (Double)obj.get("number"); if (dbl == null || dbl.doubleValue() != 100.959) { throw new Exception("Numeric value was incorrect"); } String str = (String)obj.get("attribute"); if (dbl == null || !str.equals("foo")) { throw new Exception("String attribute was incorrect"); } String jsonStr = obj.serialize(true); System.out.println(jsonStr); } catch (Exception ex) { ex.printStackTrace(); } } |
public void demoJson() { String JSON = "{\"attribute\":\"foo\", \"number\":100.959}"; try { JSONObject obj = JSONObject.parse(JSON); Double dbl = (Double)obj.get("number"); if (dbl == null || dbl.doubleValue() != 100.959) { throw new Exception("Numeric value was incorrect"); } String str = (String)obj.get("attribute"); if (dbl == null || !str.equals("foo")) { throw new Exception("String attribute was incorrect"); } String jsonStr = obj.serialize(true); System.out.println(jsonStr); } catch (Exception ex) { ex.printStackTrace(); } } |
public void demoJson2() { try { JSONObject obj = new JSONObject(); obj.put("attribute", "foo"); obj.put("number", new Double(100.959)); String jsonStr = obj.serialize(true); System.out.println(jsonStr); } catch (Exception ex) { ex.printStackTrace(); } } |