当前位置: 首页 > 知识库问答 >
问题:

JSONproblem的原因:JSONObject文本必须以字符1[duplicate]处的“{”开头

靳茂
2023-03-14
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;
    }


    public String makeCoorString() throws IOException, JSONException {
        lat = getLat();
        lng = getLng();
        String coor = lat + "," + lng;
        System.out.println(coor);
        return coor;
    }

json URL:https://maps.googleapis.com/maps/api/geocode/json?address=Roshhaayin&key=ENTER_YOUR_API_KEY

json:(我不知道它为什么会这样打印,但在我的电脑上看起来不错)

{
results: [
{
address_components: [
{
long_name: "ראש העין",
short_name: "ראש העין",
types: [
"locality",
"political"
]
},
{
long_name: "פתח תקווה",
short_name: "פתח תקווה",
types: [
"administrative_area_level_2",
"political"
]
},
{
long_name: "מחוז המרכז",
short_name: "מחוז המרכז",
types: [
"administrative_area_level_1",
"political"
]
},
{
long_name: "ישראל",
short_name: "IL",
types: [
"country",
"political"
]
}
],
formatted_address: "ראש העין, ישראל",
geometry: {
bounds: {
northeast: {
lat: 32.117834,
lng: 34.9994091
},
southwest: {
lat: 32.051418,
lng: 34.932534
}
},
location: {
lat: 32.095838,
lng: 34.952177
},
location_type: "APPROXIMATE",
viewport: {
northeast: {
lat: 32.117834,
lng: 34.9994091
},
southwest: {
lat: 32.051418,
lng: 34.932534
}
}
},
place_id: "ChIJTbFX6ioxHRURHn9WS3zh798",
types: [
"locality",
"political"
]
}
],
status: "OK"
}

共有1个答案

孟健
2023-03-14

UPD2:[表示数组,{表示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:您应该使用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;
}
 类似资料: