问题描述
所以我试图用Gson解析Java中的JSON对象。我试图从网站上使用API。
这是代码:
JSON如下所示:
[
{
id:比特币,
名称:比特币,
符号:BTC,
rank:1,
price_usd:3591.95,
price_btc:1.0,
24h_volume_usd:3263990000.0,
market_cap_usd:59516499433.0,
available_supply:16569412.0,
total_supply:16569412.0,
percent_change_1h:1.02,
percent_change_24h:15.59,
percent_change_7d:-15.18,
last_updated:1505563475
}
]
我使用Jersey来使用API,我想将这个JSON转换为一个对象。
这是应该的对象通过解析JSON来构建:
public class CryptoCurrency {
String id;
字符串名称;
字符串符号;
字符串等级;
String price_usd;
String price_btc;
字符串market_cap_usd;
字符串available_supply;
字符串total_supply;
字符串percent_change_1h;
字符串percent_change_24h;
字符串percent_change_7d;
字符串last_updated;
$ / code $ / pre
这是我的代码:
public class CoinMarketCap {
public static void main(String [] args){
Client client = ClientBuilder.newClient ();
WebTarget target = client.target(https://api.coinmarketcap.com/v1/ticker/bitcoin/);
String bitoinDeteils = target.request(MediaType.TEXT_XML).get(String.class);
Gson gson = new GsonBuilder()。create();
CryptoCurrency bitcoin = gson.fromJson(bitoinDeteils,CryptoCurrency.class);
System.out.println(比特币);
$ b
我得到一个错误
你在代码中看到什么错误吗?谢谢。
解决方案错误消息表明JSON字符串包含一个数组(was BEGIN_ARRAY ),但是你试着将它解析为一个对象(Expected BEGIN_OBJECT)。
正如我们在你提供的JSON字符串中看到的,它确实是一个数组,你感兴趣的对象被包裹在方括号( [...]
)中。
尝试将其解析为例如 List< CryptoCurrency>
然后:
Type listType = new TypeToken< ArrayList< CryptoCurrency>>(){}。getType();
列表< CryptoCurrency> list = new Gson()。fromJson(jsonString,listType);
(类型
java.lang.reflect.Type
。)
So I am trying to parse a JSON object in Java using Gson.I am trying to consume an API from a website.This is the ticker:https://api.coinmarketcap.com/v1/ticker/bitcoin/
The JSON looks like this:
[
{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "3591.95",
"price_btc": "1.0",
"24h_volume_usd": "3263990000.0",
"market_cap_usd": "59516499433.0",
"available_supply": "16569412.0",
"total_supply": "16569412.0",
"percent_change_1h": "1.02",
"percent_change_24h": "15.59",
"percent_change_7d": "-15.18",
"last_updated": "1505563475"
}
]
I am using Jersey for consuming the API and I want to transform this JSON into an object.
This is the object that should be built by parsing the JSON:
public class CryptoCurrency {
String id;
String name;
String symbol;
String rank;
String price_usd;
String price_btc;
String market_cap_usd;
String available_supply;
String total_supply;
String percent_change_1h;
String percent_change_24h;
String percent_change_7d;
String last_updated;
}
This is my code:
public class CoinMarketCap {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("https://api.coinmarketcap.com/v1/ticker/bitcoin/");
String bitoinDeteils = target.request(MediaType.TEXT_XML).get(String.class);
Gson gson = new GsonBuilder().create();
CryptoCurrency bitcoin = gson.fromJson(bitoinDeteils, CryptoCurrency.class);
System.out.println(bitcoin);
}
}
I Get an error
Do you see anything wrong in my code? thanks.
解决方案 The error message says that the JSON string contains an array ("was BEGIN_ARRAY"), but you try it parse it as an object ("Expected BEGIN_OBJECT").
As we can see in the JSON string you provided, it's indeed an array, the object you're interested in is wrapped in square brackets ([ ... ]
).
Try to parse it for example as a List<CryptoCurrency>
then:
Type listType = new TypeToken<ArrayList<CryptoCurrency>>(){}.getType();
List<CryptoCurrency> list = new Gson().fromJson(jsonString, listType);
(Type
is java.lang.reflect.Type
.)
这篇关于在JAVA中解析JSON usng Gson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!