/** * Parses a string using the default platform encoding. Either a HashMap or ArrayList is * generated. * * @param aJSONString - the string to be parsed. It is assumed that aJSONString uses the default * encoding for the platform. * @return either a HashMap or an ArrayList depending on the contents of the parameter string * @throws JSONException */ public static Object parse(String aJSONString) throws JSONException { if (aJSONString == null) { return null; } byte[] byteArray = aJSONString.getBytes(); ByteArrayInputStream theByteStream = new ByteArrayInputStream(byteArray); JSONInputStream theStream = new JSONInputStream(theByteStream); return theStream.readObject(); }
/** * Parses a string using the defined encoding. Either a HashMap or ArrayList is generated. * * @param aJSONString - the string to be parsed. For normal behavior it must be in the encoding * declared as the theEncoding parameter. * @param theEncoding - the encoding of the String passed in as aJSONString. It must be one of the * encodings declared in JSONUtilities.encoding. * @return either a HashMap or an ArrayList depending on the contents of the parameter string * @throws JSONException */ public static Object parse(String aJSONString, JSONUtilities.encoding theEncoding) throws JSONException { if (aJSONString == null) { return null; } if (theEncoding != encoding.UNICODE && theEncoding != encoding.UTF8) { throw new JSONException("Unsupported encoding: " + theEncoding); } byte[] byteArray; try { byteArray = aJSONString.getBytes(theEncoding == encoding.UNICODE ? "ISO-8859-1" : "UTF-8"); } catch (UnsupportedEncodingException e) { throw new JSONException("Unsupported encoding: " + theEncoding); } ByteArrayInputStream theByteStream = new ByteArrayInputStream(byteArray); JSONInputStream theStream = new JSONInputStream(theByteStream); return theStream.readObject(); }