从字符串获取JSON值

时间:2023-02-06 00:05:46

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

我尝试解析JSON字符串“{'test': '100.00'}”,并使用GSON库获得值:100.00。我的代码是这样的:

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 archieved?

我的结果是这样的:“100.00”,但是我只需要100.00没有引号。这怎么可能被存档?

3 个解决方案

#1


46  

double result = jobj.get("test").getAsDouble();

#2


6  

Try

试一试

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

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

get(String)方法返回JsonElement对象,您应该从中获取值。

#3


1  

If you want a number, then put a number in your JSON. Omit the needless quotes.

如果您想要一个数字,那么在JSON中添加一个数字。省略不必要的引用。

String myJSONString = "{\"test\": 100.00}";

In the JSON spec, a string is supposed to be in double quotes. You were using single quotes but I guess that works with the GSON library?

在JSON规范中,字符串应该是双引号。你用的是单引号,但我想这对GSON库有用吧?

#1


46  

double result = jobj.get("test").getAsDouble();

#2


6  

Try

试一试

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

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

get(String)方法返回JsonElement对象,您应该从中获取值。

#3


1  

If you want a number, then put a number in your JSON. Omit the needless quotes.

如果您想要一个数字,那么在JSON中添加一个数字。省略不必要的引用。

String myJSONString = "{\"test\": 100.00}";

In the JSON spec, a string is supposed to be in double quotes. You were using single quotes but I guess that works with the GSON library?

在JSON规范中,字符串应该是双引号。你用的是单引号,但我想这对GSON库有用吧?