不是Google提供的JSON对象

时间:2018-08-09 11:49:51

标签: java json gson

我正在尝试使用Google API制作翻译程序。 这就是我走了多远。

package me.kyllian.translate;

public static void main(String[] args) throws Exception{
    System.setProperty("http.agent", "Chrome");
    String link = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=%from%&tl=%to%&dt=t&q=%text%";
    Scanner reader = new Scanner(System.in);
    System.out.println("What to translate? :");
    String input = reader.next();
    reader.close();
    URL url = new URL(link.replace("%from%", "en").replace("%to%", "nl").replace("%text%", input));
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("GET");

    connection.connect();
    JsonParser jsonParser = new JsonParser();
    JsonElement jsonElement = jsonParser.parse(new InputStreamReader((InputStream) connection.getContent()));
    JsonObject obj = jsonElement.getAsJsonObject();

    String routeID = null;
    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) routeID = obj.get("Route").getAsString();
}

我的JSON字符串是:

[[[[“ Goedemorgen”,“ goodmorning”,null,null,1]],null,“ en”]

但是它似乎不是JSON对象,有没有办法解决这个问题?

0 个答案:

没有答案