Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert String to JsonObject

I am using a httprequest to get Json from a web into a string.

It is probably quite simple, but I cannot seem to convert this string to a javax.json.JsonObject.

How can I do this?

like image 475
Revils Avatar asked Sep 20 '14 10:09

Revils


People also ask

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 convert a string to a JSON object in Python?

you can turn it into JSON in Python using the json. loads() function. The json. loads() function accepts as input a valid string and converts it to a Python dictionary.

How do you pass a JSON object into 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.

How do you parse a JSON object in Java?

We can convert a JSON to Java Object using the readValue() method of ObjectMapper class, this method deserializes a JSON content from given JSON content String.


2 Answers

JsonReader jsonReader = Json.createReader(new StringReader("{}")); JsonObject object = jsonReader.readObject(); jsonReader.close(); 

See docs and examples.

like image 92
fracz Avatar answered Sep 29 '22 01:09

fracz


Since the above reviewer didn't like my edits, here is something you can copy and paste into your own code:

private static JsonObject jsonFromString(String jsonObjectStr) {      JsonReader jsonReader = Json.createReader(new StringReader(jsonObjectStr));     JsonObject object = jsonReader.readObject();     jsonReader.close();      return object; } 
like image 38
Sridhar Sarnobat Avatar answered Sep 29 '22 01:09

Sridhar Sarnobat