第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么 JSON 問題讓我困惑:JSONObject 文本必須以字符 1 處的“{”開頭

為什么 JSON 問題讓我困惑:JSONObject 文本必須以字符 1 處的“{”開頭

海綿寶寶撒 2023-10-19 21:34:00
我使用谷歌地圖 API 從谷歌地圖 API 獲取 GPS 坐標(biāo)。我使用與我的老師代碼幾乎相似的代碼,用于使用 api 中的 JSON。他使用來自 darksky 的 api,我使用來自谷歌地圖的 API。我的代碼:package com.company;import org.json.JSONException;import org.json.JSONObject;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.URL;import java.nio.charset.Charset;public class MyCoorGiver {public final String API_URL = "https://maps.googleapis.com/maps/api/geocode/json?address=";private String cityName;private final String apiKey = "&key=";private String fullUrl;private String rawData;JSONObject jsonObject;private double lat;private double lng;public String getFullUrl() {    return fullUrl;}public MyCoorGiver(String cityName) {    this.cityName = cityName;    this.fullUrl = this.API_URL + cityName + this.apiKey;}private JSONObject readFromJsonUrl() throws IOException, JSONException {    InputStream is = new URL(this.fullUrl).openStream();    BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));    String jsonText = readAll(br);    jsonObject = new JSONObject(jsonText);    return jsonObject;}    public String readAll(BufferedReader br) throws IOException {        StringBuilder sb = new StringBuilder();        int cp;        while((cp = br.read()) != -1){            sb.append(cp);        }        rawData = sb.toString();        return sb.toString();    }    public String getRaw(){        return rawData;    }    private double getLat() throws IOException, JSONException {        JSONObject myData = readFromJsonUrl();        JSONObject location = myData.getJSONObject("location");        double lat = location.getDouble("lat");        return lat;    }    private double getLng() throws IOException, JSONException {        JSONObject myData = readFromJsonUrl();        JSONObject location = myData.getJSONObject("location");        double lng = location.getDouble("lng");        return lng;    }
查看完整描述

1 回答

?
米脂

TA貢獻(xiàn)1836條經(jīng)驗 獲得超3個贊

UPD2: [表示數(shù)組,{在JSON語法中表示對象。試試。


private double getLat() throws IOException, JSONException {

        JSONObject myData = readFromJsonUrl();

        JSONArray results = myData.getJSONArray("results");

        JSONObject geometry = results.getJSONObject(0).getJSONObject("geometry");

        JSONObject location = geometry.getJSONObject("location");

        double lat = location.getDouble("lat");

        return lat;

    }

UPD:您應(yīng)該使用readLine()方法而不是read(),嘗試更改您的代碼,如下所示。


public String readAll(BufferedReader br) throws IOException {

        StringBuilder sb = new StringBuilder();

        String cp;

        while ((cp = br.readLine()) != null) {

            sb.append(cp);

        }

        return sb.toString();

    }

您可以jsonText在控制臺中打印以查看 JSON 語法是否正確。也許你可以嘗試使用這段代碼。


private JSONObject readFromJsonUrl() throws IOException, JSONException {

    InputStream is = new URL(this.fullUrl).openStream();

    BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));

    String jsonText = readAll(br);

    //jsonObject = new JSONObject(jsonText);

    ObjectMapper objectMapper = new ObjectMapper();

    JSONObject jsonObject = objectMapper.readValue(jsonText, JSONObject.class);

    return jsonObject;

}


查看完整回答
反對 回復(fù) 2023-10-19
  • 1 回答
  • 0 關(guān)注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號