0
"papers": [
{
  "files": [
    {
      "url": "http://farnsworth.papro.org.uk/file/977", 
      "type": "Initial XML version of the paper", 
      "name": "c6938201-dac0-4ef9-91cd-ca6e8c30f4b8.xml"
    }, 
    {
      "url": "http://farnsworth.papro.org.uk/file/978", 
      "type": "Final annotated XML file", 
      "name": "c6938201-dac0-4ef9-91cd-ca6e8c30f4b8_final.xml"
    }
  ]

How can i get the first url in this json file ? What I have so far is:

                JSONObject file = (JSONObject) files.get(j);

                String url = (String) file.get("url");
                System.out.println(url);
fumzy
  • 7
  • 5
  • http://www.json.org/javadoc/org/json/JSONObject.html – jpumford Jun 15 '13 at 23:50
  • Aside from "Don't use that ancient crufty json.org library" there are *numerous* examples of parsing JSON in Java here on SO that would get you further than you are. – Brian Roach Jun 15 '13 at 23:52
  • http://stackoverflow.com/questions/7332611/how-do-i-extract-value-from-json?rq=1 ... http://stackoverflow.com/questions/8939250/parsing-json-file-java?lq=1 would also be of interest. – Brian Roach Jun 15 '13 at 23:55
  • currently am getting the two url but i can't figure out how to get just the first url in the file. I don't know if there is something like `.first()`? – fumzy Jun 15 '13 at 23:56
  • I have carefully examine all this links before posting this question, but none seems to have an answer to my question. – fumzy Jun 16 '13 at 00:00
  • you can fetch this files object as a JSONArray instead of JSONObject. The advantage being that, you get array.get(index) method for the JSONArray. – voidMainReturn Jun 16 '13 at 00:09

1 Answers1

1

something like this should work :

JSONArray results = d.getJSONArray("files");
JSONObject p = (JSONObject)results.get(0);
url = p.getString("url");
voidMainReturn
  • 3,339
  • 6
  • 38
  • 66