NotSure
NotSure

Reputation: 681

Parsing JSON in JAVA usng Gson

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

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224) at com.google.gson.Gson.fromJson(Gson.java:887) at com.google.gson.Gson.fromJson(Gson.java:852) at com.google.gson.Gson.fromJson(Gson.java:801) at com.google.gson.Gson.fromJson(Gson.java:773) at com.randomizer.CoinMarketCap.main(CoinMarketCap.java:21) Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213) ... 5 more

Do you see anything wrong in my code? thanks.

Upvotes: 0

Views: 604

Answers (1)

juzraai
juzraai

Reputation: 5943

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.)

Upvotes: 1

Related Questions