gpt4 book ai didi

java - 我的 Java JSON 解析器出了什么问题?

转载 作者:行者123 更新时间:2023-12-02 05:15:41 24 4
gpt4 key购买 nike

我对解析 JSON 非常陌生,我正在使用这里的解析器 https://github.com/douglascrockford/JSON-java/ 。我正在尝试解析来自《纽约时报》的数据,特别是“docs”中的“web_urls”和“main”,但出现此错误:

Exception in thread "main" JSON.JSONException: JSONObject["docs"] not found.
at JSON.JSONObject.get(JSONObject.java:475)
at JSON.JSONObject.getJSONArray(JSONObject.java:557)
at News.sendGet(News.java:53)
at News.main(News.java:80)

Exception in thread "main" JSON.JSONException: JSONObject["main"] not found.
at JSON.JSONObject.get(JSONObject.java:475)
at JSON.JSONObject.getString(JSONObject.java:656)
at News.sendGet(News.java:56)
at News.main(News.java:78)

这是 JSON 的 URL:http://api.nytimes.com/svc/search/v2/articlesearch.json?q=water&begin_date=20141101&end_date=20141101&api-key=sample-key

下面是我的代码:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import JSON.JSONObject;


public class News
{
/* Instance Field */
private final static String apiNYT = "###"; // api key for NYTimes

/**
* This is an HTTP Get Request to retrieve articles from
* @throws Exception
* @param url request
*/
private void sendGet(String url) throws Exception
{
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

// Read the JSON
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;

// Response is the JSON form of type StringBuffer
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

// jstr is the string version of the JSON form
String jstr = response.toString();


//print result
System.out.println(jstr);

JSONObject jsonObj = new JSONObject(jstr);

// arr is a JSONArray that records the docs array
JSON.JSONArray arr = jsonObj.getJSONArray("docs");
String blurb = "";
String title = "";

// Loop through the docs array
for (int i = 0; i < arr.length(); i++)
{
blurb += arr.getJSONObject(i).getString("web_url");
title += arr.getJSONObject(i).getJSONObject("headline").getString("main");
}

System.out.println(blurb);
System.out.println(title);
}

/**
* Main method
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{

News http = new News();

// Test call for NYTimes API (articles with the word "water" in title on 14/11/01)
String url = "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=water&begin_date=20141101&end_date=20141101&api-key="
+ apiNYT ;

http.sendGet(url);
}
}

最佳答案

尝试这样:

JSON.JSONArray arr = jsonObj.getJSONObject("response").getJSONArray("docs");

docs数组嵌套在 response 中对象,因此需要首先处理该级别。您可能希望将其分成多行并添加一些空检查。

关于java - 我的 Java JSON 解析器出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26965002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com