用于NYTimes API的JSON Parser in Java

时间:2014-11-17 00:54:07

标签: java json api

我是解析JSON 的新手,我不明白为什么我无法解析我从NYTimes API获得的JSON表单。我以为我正在遵循“响应”的层次结构---> “docs”----> “web_url”,但我收到了一个错误。我希望能够在表单中获得所有“web_url”。

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)

0 个答案:

没有答案