Converting JSON String to JSONObject in Java
If you encounter difficulties in converting a JSON string to a JSONObject, consider the following straightforward solution. Suppose you have a JSON string stored in a String variable called jsonString:
{ "phonetype": "N95", "cat": "WP" }
Using the org.json Library:
To access the functionality of the org.json library, follow these steps:
try { JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}"); }catch (JSONException err){ Log.d("Error", err.toString()); }
By using this approach, you can seamlessly convert a JSON string into a valid JSONObject object.
The above is the detailed content of How to Convert a JSON String to a JSONObject in Java?. For more information, please follow other related articles on the PHP Chinese website!