Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert jsonString to JSONObject in Java

I have String variable called jsonString:

{"phonetype":"N95","cat":"WP"} 

Now I want to convert it into JSON Object. I searched more on Google but didn't get any expected answers!

like image 987
Mr. Sajid Shaikh Avatar asked Mar 09 '11 12:03

Mr. Sajid Shaikh


People also ask

Can we convert JSONArray to JSONObject?

We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.

Can we convert string to object in Java?

We can also convert the string to an object using the Class. forName() method. Parameter: This method accepts the parameter className which is the Class for which its instance is required.

How do you assign a JSON object to a string in Java?

JSONObject json= (JSONObject) JSONValue. parse(jsonData); JSONObject data = (JSONObject) json. get("data"); After you have parsed the json data, you need to access the data object later get "map" data to json string.


1 Answers

Using org.json library:

try {      JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}"); }catch (JSONException err){      Log.d("Error", err.toString()); } 
like image 75
dogbane Avatar answered Oct 09 '22 02:10

dogbane