无法从url解析JSON

时间:2016-04-03 11:58:33

标签: java json java-ee jackson gson

编写一段代码,用于查询返回JSON的URL,并解析JSON字符串以提取信息。应该解析和返回的信息是pageid和“另请参见”链接列表。这些链接应格式化为可供个人用于查找相应文章的实际链接。 使用Wikipedia API进行查询。示例查询是:

URL

可以生成其他查询,更改查询字符串的“标题”部分。解析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();
        }


          }
}

我在eclipse中使用了以下链接中的json jar: Json jar

当我运行上面的代码时,我收到以下错误;

org.json.JSONException: JSONObject["pageid"] not found.
at org.json.JSONObject.get(JSONObject.java:471)
at JsonRead.main(JsonRead.java:35)

如何从网址中提取pageid的详细信息以及“另请参阅”链接? 我之前从未参与过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]]"
               }
            ]
         }
      }
   }
}

2 个答案:

答案 0 :(得分: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中实际收到的内容。

答案 1 :(得分:1)

调用org.json.JSONException: JSONObject["pageid"] not found.时会收到异常json.get("pageid"),因为pageid不是根的直接子元素。你必须在对象图中一直向下:

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。您必须手动解析它。