JSONを返すURLを照会するコードを記述し、JSON文字列を解析して情報を取り出すことができます。解析され、返されるべき情報は、pageidと "See Also"リンクのリストです。これらのリンクは、適切な記事を見つけるために人が使用できる実際のリンクになるようにフォーマットする必要があります。 クエリにWikipedia APIを使用します。サンプルクエリは次のとおりです。URLからJSONを解析できません
その他のクエリは、クエリ文字列の「タイトル」の部分を変更生成することができます。 JSONを解析して「参照」リンクを引くコードは、Wikipediaのすべての記事を操作するのに十分な一般的なものでなければなりません。 ; Json jar
私は、私は以下のエラーを取得しています上記のコードを実行 :私は日食に下記のリンクからJSONジャーを使用していた
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonRead {
private static String readUrl(String urlString) throws Exception {
BufferedReader reader = null;
try {
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);
return buffer.toString();
} finally {
if (reader != null)
reader.close();
}
}
public static void main(String[] args) throws IOException, JSONException {
JSONObject json;
try {
json = new JSONObject(readUrl("https://en.wikipedia.org/w/api.php?format=json&action=query&titles=SMALL&prop=revisions&rvprop=content"));
System.out.println(json.toString());
System.out.println(json.get("pageid"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
:
は、私は以下のコードを書いてみました
org.json.JSONException: JSONObject["pageid"] not found.
at org.json.JSONObject.get(JSONObject.java:471)
at JsonRead.main(JsonRead.java:35)
どのようにしてたpageIDの詳細ともされたURLから、「関連項目」のリンクを抽出することができますか? 私は私が
ここでJSONを続行する方法を知っているので、親切にする前にJSONに働いたことはない:
{
"batchcomplete":"",
"query":{
"pages":{
"1808130":{
"pageid":1808130,
"ns":0,
"title":"SMALL",
"revisions":[
{
"contentformat":"text/x-wiki",
"contentmodel":"wikitext",
"*":"{{About|the ALGOL-like programming language|the scripting language formerly named Small|Pawn (scripting language)}}\n\n'''SMALL''', Small Machine Algol Like Language, is a [[computer programming|programming]] [[programming language|language]] developed by Dr. [[Nevil Brownlee]] of [[Auckland University]].\n\n==History==\nThe aim of the language was to enable people to write [[ALGOL]]-like code that ran on a small machine. It also included the '''string''' type for easier text manipulation.\n\nSMALL was used extensively from about 1980 to 1985 at [[Auckland University]] as a programming teaching aid, and for some internal projects. Originally written to run on a [[Burroughs Corporation]] B6700 [[Main frame]] in [[Fortran]] IV, subsequently rewritten in SMALL and ported to a DEC [[PDP-10]] Architecture (on the [[Operating System]] [[TOPS-10]]) and IBM S360 Architecture (on the Operating System VM/[[Conversational Monitor System|CMS]]).\n\nAbout 1985, SMALL had some [[Object-oriented programming|object-oriented]] features added to handle structures (that were missing from the early language), and to formalise file manipulation operations.\n\n==See also==\n*[[ALGOL]]\n*[[Lua (programming language)]]\n*[[Squirrel (programming language)]]\n\n==References==\n*[http://www.caida.org/home/seniorstaff/nevil.xml Nevil Brownlee]\n\n[[Category:Algol programming language family]]\n[[Category:Systems programming languages]]\n[[Category:Procedural programming languages]]\n[[Category:Object-oriented programming languages]]\n[[Category:Programming languages created in the 1980s]]"
}
]
}
}
}
}
あなたはアプリケーションをデバッグしましたか?実際に何を取得しようとしました。文字列変数 'jsonText'が含まれています。 –
' jsonText = readAll(rd); 'の後に' jsonText'を表示しようとしました。何が間違っているのを参照してください – Dimi
私はそれをデバッグしようとし、jsonTextが空であることがわかりましたが、whtがここで問題であるのを知らないのですか? – user2077648