Java中NYTimes API的JSON解析器
我对解析JSON非常陌生,我不明白为什么我不能解析我从NYTimes API获得的JSON表单。我以为我遵循的是“响应”-->“文档”-->“web\uURL”的层次结构,但我得到了一个错误。我希望能够获得表单中的所有“web_url”Java中NYTimes API的JSON解析器,java,json,api,Java,Json,Api,我对解析JSON非常陌生,我不明白为什么我不能解析我从NYTimes API获得的JSON表单。我以为我遵循的是“响应”-->“文档”-->“web\uURL”的层次结构,但我得到了一个错误。我希望能够获得表单中的所有“web_url” import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import JSON.JSONException;
import JSON.JSONObject;
public class TestJSONParse {
public static void main(String[] args) throws FileNotFoundException, JSONException {
String jsonData = "";
BufferedReader br = null;
try {
String line;
br = new BufferedReader(new FileReader("/Users/Desktop/nytimes.txt"));
while ((line = br.readLine()) != null) {
jsonData += line + "\n";
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
JSONObject obj = new JSONObject(jsonData);
System.out.println("nytimes:\n" + obj.getJSONObject("response").getJSONObject("docs").getString("web_url"));
}
}
以下是文件的外观部分:
{
"response": {
"meta": {
"hits": 21,
"time": 361,
"offset": 0
},
"docs": [{
"web_url": "http:\/\/www.nytimes.com\/reuters\/2014\/11\/01\/world\/europe\/01reuters-ireland-politics.html",
"snippet": "Tens of thousands of people marched in towns across Ireland on Saturday in a second day of mass protests against water charges, the biggest display of opposition to government austerity measures since the country's banking crisis and bailout in 2010....",
"lead_paragraph": "Tens of thousands of people marched in towns across Ireland on Saturday in a second day of mass protests against water charges, the biggest display of opposition to government austerity measures since the country's banking crisis and bailout in 2010. ",
"abstract": null,
"print_page": null,
"blog": [],
"source": "Reuters",
"multimedia": [],
"headline": {
"main": "Mass Water Protests Put Pressure on Irish Government",
"print_headline": "Mass Water Protests Put Pressure on Irish Government"
},
"keywords": [],
"pub_date": "2014-11-01T13:21:35Z",
"document_type": "article",
"news_desk": "None",
"section_name": "World",
"subsection_name": "Europe",
"byline": {
"person": [],
"original": "By REUTERS",
"organization": "REUTERS"
},
"type_of_material": "News",
"_id": "5455176038f0d84a08fb19f9",
"word_count": "543"
}, {
"web_url": "http:\/\/www.nytimes.com\/aponline\/2014\/11\/01\/world\/europe\/ap-eu-ireland-water-protests.html",
"snippet": "Organizers say at least 100,000 people are marching against Ireland's new tax on water, a charge imposed as part of the country's successful exit from an international bailout.",
"lead_paragraph": "Organizers say at least 100,000 people are marching against Ireland's new tax on water, a charge imposed as part of the country's successful exit from an international bailout.",
"abstract": null,
"print_page": null,
"blog": [],
"source": "AP",
"multimedia": [],
"headline": {
"main": "Marchers Protest Ireland's New Tax on Water Supply",
"print_headline": "Marchers Protest Ireland's New Tax on Water Supply"
},
"keywords": [],
"pub_date": "2014-11-01T10:58:03Z",
"document_type": "article",
"news_desk": "None",
"section_name": "World",
"subsection_name": "Europe",
"byline": {
"person": [],
"original": "By THE ASSOCIATED PRESS",
"organization": "THE ASSOCIATED PRESS"
},
"type_of_material": "News",
"_id": "5454f5aa38f0d839202eac78",
"word_count": "128"
}
}
这就是我得到的错误:
Exception in thread "main" JSON.JSONException: Missing value at 1 [character 2 line 1]
at JSON.JSONTokener.syntaxError(JSONTokener.java:433)
at JSON.JSONTokener.nextValue(JSONTokener.java:388)
at JSON.JSONObject.<init>(JSONObject.java:207)
at JSON.JSONObject.<init>(JSONObject.java:323)
at TestJSONParse.main(TestJSONParse.java:38)
线程“main”JSON.JSONException中的异常:1处缺少值[字符2第1行]
位于JSON.JSONTokener.syntaxError(JSONTokener.java:433)
位于JSON.JSONTokener.nextValue(JSONTokener.java:388)
在JSON.JSONObject.(JSONObject.java:207)
位于JSON.JSONObject。(JSONObject.java:323)
位于TestJSONParse.main(TestJSONParse.java:38)
打印JSONData并确保它看起来正常。此外,可能是文件开头有一个字节顺序标记被读取序列弄乱了,或者解析器不明白如何处理。当我打印JSONData时,我在表单前面看到了这个标记,这可能是您描述的被读取序列弄乱的:{\rtf1\ansi\ansicpg1252\cooart1265\cocooasubrtf210{\fonttbl\f0\fmodern\fcharset0 Courier;}{\colortbl;\red255\green255\blue255;}\margl1440\margr1440\vieww10800\viewh8400\viewkind0\deftab720\pard\pardeftab720\f0\fs24\cf0\。在这种情况下我该怎么办?这是您正在使用的解析器吗?(来自json.org?)不,那不是BOM(只有2-3个字节),这是垃圾。您的下载操作有问题。我认为您的下载操作没有为您提供有效的json。我能够获取您的原始输入字符串(预编辑),并使用我在前面的注释中链接的解析器对其进行解析。