0

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.

juzraai
  • 5,693
  • 8
  • 33
  • 47
NotSure
  • 651
  • 2
  • 7
  • 24
  • Please show an example JSON too. It seems at some point the JSON contains an array but you try to store it in an object. – juzraai Sep 16 '17 at 10:51
  • Possible duplicate of [How to deserialize a JSON array using Gson](https://stackoverflow.com/questions/10292514/how-to-deserialize-a-json-array-using-gson). Your JSON is an array containing just one object, so you need to deserialize the array. – Aleksei Budiak Sep 16 '17 at 11:04
  • 1) Why are you requesting XML and trying to parse JSON? 2) Print `bitcoinDetails`. What is it? – Paul Samsotha Sep 16 '17 at 11:49
  • this is the JSON as string. I want to deserialize it into an object. – NotSure Sep 16 '17 at 12:12

1 Answers1

1

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

juzraai
  • 5,693
  • 8
  • 33
  • 47
  • Yes you are right. I was new to JSON to I didn't even see that the only object is inside an array. I've ended up just taking my code and making my Wrapper (named CryptoCurrency) an array. what I changed was : CryptoCurrency[] arr = gson.fromJson(bitoinDeteils, CryptoCurrency[].class); and then just accesing CryptoCurrency[0] – NotSure Sep 16 '17 at 12:46