Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GSON - Get JSON value from String

I'm trying to parse the JSON String "{'test': '100.00'}" and in order to get the value: 100.00 with the GSON library. My code looks like this:

String myJSONString = "{'test': '100.00'}"; JsonObject jobj = new Gson().fromJson(myJSONString, JsonObject.class);  String result = jobj.get("test").toString();  System.out.println(result); 

My result looks like this: "100.00", but I would need just 100.00 without the quotes. How can this be achieved?

like image 845
jan Avatar asked Nov 22 '13 19:11

jan


People also ask

How do I get GSON value?

String myJSONString = "{'test': '100.00'}"; JsonObject jobj = new Gson(). fromJson(myJSONString, JsonObject. class); String result = jobj. get("test").

What is JsonElement in GSON?

A class representing an element of Json. It could either be a JsonObject , a JsonArray , a JsonPrimitive or a JsonNull .

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.


2 Answers

double result = jobj.get("test").getAsDouble(); 
like image 191
Sanj Avatar answered Oct 13 '22 10:10

Sanj


Try

String result = jobj.get("test").getAsString(); 

get(String) method returns JsonElement object which you then should get the value from.

like image 28
dimoniy Avatar answered Oct 13 '22 10:10

dimoniy