我正在使用Bing的自动建议功能在给定查询的情况下自动建议我的术语。您可以在这里找到该工具:http://api.bing.com/osjson.aspx?query=pe,正如您所看到的,它返回的是一种奇怪的格式,这种格式并不完全是JSON。这是不是与JSON不同的特定标准?我尝试将其解析为JSON,使用...
InputStream i = new URL(url).openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(i, Charset.forName("UTF-8")));
JSONObject json = new JSONObject(readAll(reader));但是我得到了错误A JSONObject text must begin with '{' found:" at 2 [character 3 line 1]
readAll =
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}发布于 2011-10-03 18:44:21
您的示例是有效的JSON:
["pe",["people","people search","petsmart","petco","petfinder","pep boys","people finder","people of walmart"]]它不是对象,它是数组,它在第一个位置包含字符串,在第二个位置包含另一个数组。因此,尝试以JSONArray而不是JSONObject的身份进行解析。
发布于 2011-10-03 19:04:41
JSON对象以{开头,以}结束,JSONObject类被设计为解析JSON。JSON Array以[开头,以]结束,JSONArray类被设计用来解析它。
我希望这能帮到你。
https://stackoverflow.com/questions/7633736
复制相似问题