为什么我得到JSONObject文本必须在字符1处以'{'开头 [英] Why I get A JSONObject text must begin with '{' at character 1

查看:81
本文介绍了为什么我得到JSONObject文本必须在字符1处以'{'开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从json获取信息. 使用Google Maps API(地理编码) 即时通讯检查intellij,我得到了:JSONObject文本必须在字符1处以'{'开头 即使我使用老师的方式处理json. 他的代码工作与其几乎100%相同. 我用需要从用户获取信息的构造函数编辑" URL. 我可以获取完整的URL,它会将我带到json文件,似乎一切正常.但我无法像在immakecoor ...方法中尝试的那样使用它来使工作变得更顺畅

Im trying to get a information from a json. using google maps api (geocoding) im checking on intellij and i get the : JSONObject text must begin with '{' at character 1 even though I use my teacher way of working with jsons. his code work and its almost 100% the same. Im "editing" the url with constructor that need to get info from the user. I can get the full url and it takes me to the json file and it seems everything is okay. but I cant use it to make coor like im trying in the makecoor... method

我的json阅读器代码:

My json reader code:

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 andKey = "&key=";
  private final String apiKey = "";
  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.andKey + 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:

{
    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"
}

推荐答案

尝试从json文件中删除{之前的空格吗?

Try to remove the space before the { from the json file ?

这篇关于为什么我得到JSONObject文本必须在字符1处以'{'开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆