JSON BEGIN_OBJECT error

451 views
Skip to first unread message

Derek Hill

unread,
Apr 16, 2018, 9:09:05 PM4/16/18
to google-gson

Im experimenting with JSON and im trying to pull specific information...

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

public class Json {
    public static String toPrettyFormat(String jsonString)
    {
        JsonParser parser = new JsonParser();
        JsonObject json = parser.parse(jsonString).getAsJsonObject();

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String prettyJson = gson.toJson(json);

        return prettyJson;
    }
    public static void main(String[] args) {
        try {
            URL url = new URL("https://api.coindesk.com/v1/bpi/currentprice/CAD.json");
            System.setProperty("http.agent", "Chrome");
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
            String str = "";
            String msg = "";
            while (null != (str = br.readLine())) {
                msg = msg + str;
                System.out.println("Test String 1 : "+toPrettyFormat(str));
            }
            System.out.println("Test String 2 : "+msg);
            Gson gson = new Gson();
            System.out.println("Test String 3 : "+str);
            JClass jClass = gson.fromJson(msg,JClass.class);
            System.out.println("Test String 4 : "+jClass.bpi);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

JClass.java

public class JClass {
    public String time, disclaimer, bpi, CAD, USD, code, rate, description, rate_float;

    public JClass(String time, String disclaimer, String bpi, String USD, String CAD, String code, String rate, String description, String rate_float){
        this.time = time;
        this.disclaimer = disclaimer;
        this.bpi = bpi;
        this.USD = USD;
        this.CAD = CAD;
        this.code = code;
        this.rate = rate;
        this.description = description;
        this.rate_float = rate_float;
    }
}

Output

Test String 1 : {
  "time": {
    "updated": "Apr 16, 2018 23:19:00 UTC",
    "updatedISO": "2018-04-16T23:19:00+00:00",
    "updateduk": "Apr 17, 2018 at 00:19 BST"
  },
  "disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
  "bpi": {
    "USD": {
      "code": "USD",
      "rate": "8,041.1538",
      "description": "United States Dollar",
      "rate_float": 8041.1538
    },
    "CAD": {
      "code": "CAD",
      "rate": "10,107.3282",
      "description": "Canadian Dollar",
      "rate_float": 10107.3282
    }
  }
}
Test String 2 : {"time":{"updated":"Apr 16, 2018 23:19:00 UTC","updatedISO":"2018-04-16T23:19:00+00:00","updateduk":"Apr 17, 2018 at 00:19 BST"},"disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org","bpi":{"USD":{"code":"USD","rate":"8,041.1538","description":"United States Dollar","rate_float":8041.1538},"CAD":{"code":"CAD","rate":"10,107.3282","description":"Canadian Dollar","rate_float":10107.3282}}}
Test String 3 : null
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 10 path $.time
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
    at com.google.gson.Gson.fromJson(Gson.java:888)
    at com.google.gson.Gson.fromJson(Gson.java:853)
    at com.google.gson.Gson.fromJson(Gson.java:802)
    at com.google.gson.Gson.fromJson(Gson.java:774)
    at wallettemplate.Json.main(Json.java:37)
Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 10 path $.time
    at com.google.gson.stream.JsonReader.nextString(JsonReader.java:825)
    at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:401)
    at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:389)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
    ... 5 more

Process finished with exit code 0

What im trying to pull is the the rate in a currency...And im getting an error that i dont quite understand..

Im still learning java so i may be lacking in some key terms or am not aware of how to properly perform said desired result..

Reply all
Reply to author
Forward
0 new messages