2016-04-03 147 views
0

编写一段代码,将查询返回JSON的URL并解析JSON字符串以提取信息。应该解析并返回的信息是pageid和“另请参阅”链接的列表。这些链接应该被格式化为一个人可以用来找到适当文章的实际链接。 使用维基百科API进行查询。样品查询是:无法解析URL中的JSON

URL

其他查询可以产生改变的查询字符串的“标题”部分。解析JSON并拉取“See Also”链接的代码应该足够通用,可以用于任何维基百科文章。

我试着写了下面的代码:

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(); 
     } 


      } 
} 

我已经在Eclipse中使用JSON罐子从下面的链接: Json jar

当我跑我提示以下错误:上面的代码;

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]]" 
       } 
      ] 
     } 
     } 
    } 
} 
+0

你有调试应用程序吗?并试图得到实际的字符串变量'jsonText'包含.. –

+1

尝试在'String jsonText = readAll(rd);'之后打印出'jsonText''因为错误说你的字符串不是以{开头的,所以你看看有什么不对 – Dimi

+1

我试着调试它,发现jsonText是空的,但不知道这里的问题是什么? – user2077648

回答

1

由于pageid不是您的根的直接子元素,因此在调用json.get("pageid")时会得到异常org.json.JSONException: JSONObject["pageid"] not found.。你必须通过对象图向下走了一路:

int pid = json.getJSONObject("query") 
     .getJSONObject("pages") 
     .getJSONObject("1808130") 
     .getInt("pageid"); 

如果你在那里有一个array你甚至会遍历数组元素(或挑一个你想要的)。

编辑这里的代码即可获得含有该领域的“又见”价值

String s = json.getJSONObject("query") 
     .getJSONObject("pages") 
     .getJSONObject("1808130") 
     .getJSONArray("revisions") 
     .getJSONObject(0) 
     .getString("*"); 

结果字符串不包含有效的JSON。你将不得不手动解析它。

+0

是的,你是对的,但对于这个JSON我怎么才能找到另请参见值 – user2077648

+0

@ user2077648更新了我的答案 – nyname00

2

如果你读Exception仔细,你会发现你在自己的解决方案。

Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1] 
at org.json.JSONTokener.syntaxError(JSONTokener.java:433) 

ExceptionA JSONObject text must begin with '{'这意味着你从api收到的JSON可能是不正确的。

所以,我建议你调试你的代码,并尝试找出你实际收到的字符串变量jsonText

+0

我无法从URL中接收字符串,请问为什么会出现这种情况,因为相同的代码对其他ursl的工作正常? – user2077648

+0

我认为你的'URL'可能有问题,它既没有得到一个很好接收的输入数据,也没有任何异常可能由此引发。或者可能会发生更多的事情。不能确切地说@ user2077648 –

+0

我调试了我的代码,现在数据被提取,但我不能解析它仍然, – user2077648