0

Write a piece of code that will query a URL that returns JSON and can parse the JSON string to pull out pieces of information. The information that should be parsed and returned is the pageid and the list of “See Also” links. Those links should be formatted to be actual links that can be used by a person to find the appropriate article. Use the Wikipedia API for the query. A sample query is:

URL

Other queries can be generated changing the “titles” portion of the query string. The code to parse the JSON and pull the “See Also” links should be generic enough to work on any Wikipedia article.

I tried writing the below code:

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


          }
}

I have used the json jar from the below link in eclipse: Json jar

When I run the above code I am getting the below error;

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

How can I extract the details of the pageid and also the "See Also" links from the url? I have never worked on JSON before hence kindly let me know how to proceed here

The 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]]"
               }
            ]
         }
      }
   }
}
6
  • have you debug the application?? and tried to get what actually String variable ` jsonText` Contains.. Commented Apr 3, 2016 at 12:04
  • 1
    try to print out jsonText after String jsonText = readAll(rd); because the error says, that your String doesn't start with {, so you see whats wrong Commented Apr 3, 2016 at 12:08
  • 1
    I tried debugging it and found that jsonText is empty, but dont know wht is the problem here? Commented Apr 3, 2016 at 12:11
  • then there must be a problem with some another piece of code . Please debug and gothrough with whole code nd found the bug itself Commented Apr 3, 2016 at 12:17
  • Not sure why you added Gson or Jackson tags since you aren't using those Commented Apr 3, 2016 at 17:38

2 Answers 2

2

If You Read your Exception Carefully you will find your solution at your own.

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)

Your Exception says A JSONObject text must begin with '{' it means the the json you received from the api is probably not Correct.

So, I suggest you to debug your code and try to find out what you actually received in your String Variable jsonText.

Sign up to request clarification or add additional context in comments.

3 Comments

I am not able to receive the string from the URL, could you please suggest why this happens as the same code works fine for other ursl's??
I think there may be a problem with your URL which doesn't either get a well received input data or any exception may raised due to this. Or there may be many more thing may happen . can't say exactly @user2077648
I have debugged my code and now data is fetched but I am not able to parse it still,
1

You get the exception org.json.JSONException: JSONObject["pageid"] not found. when calling json.get("pageid") because pageid is not a direct sub-element of your root. You have to go all the way down through the object graph:

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

If you have an array in there you will even have to iterate the array elements (or pick the one you want).

Edit Here's the code to get the field containing the 'see also' values

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

The resulting string contains no valid JSON. You will have to parse it manually.

1 Comment

Yes you are right but for this json how can I find the See Also values??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.