2016-02-22 67 views
0

如果不使用額外的JAR或API,嚴格使用javax.json.*,如何從URL讀取JSON對象?如何使用JsonReader從URL讀取JSON對象?

javadocs認爲這是可能的:

JsonReader jsonReader = Json.createReader(new StringReader("[]")); 
JsonArray array = jsonReader.readArray(); 
jsonReader.close(); 

,但不是具體的關於如何使用URL or URI

當前代碼:

package net.bounceme.noagenda; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.StringReader; 
import javax.json.JsonObject; 
import static java.lang.System.out; 
import java.net.URL; 
import java.net.URLConnection; 
import java.nio.charset.Charset; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.json.Json; 
import javax.json.JsonArray; 
import javax.json.JsonReader; 

public class NoAgenda { 

    public static void main(String[] args) { 
     new NoAgenda().fromURL(); 
    } 

    private void fromURL() { 
     try { 
      String jsonString = callURL("http://www.javascriptkit.com/dhtmltutors/javascriptkit.json"); 
     } catch (IOException ex) { 
      Logger.getLogger(NoAgenda.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     try (JsonReader jsonReader = Json.createReader(new StringReader("[]"))) { 
      JsonArray array = jsonReader.readArray(); 
      out.println(array.toString()); 
     } 
    } 

    public String callURL(String myURL) throws IOException { 
     System.out.println("Requested URL:" + myURL); 
     StringBuilder sb = new StringBuilder(); 
     URLConnection urlConn = null; 
     InputStreamReader in = null; 
     URL url = new URL(myURL); 
     urlConn = url.openConnection(); 
     if (urlConn != null) { 
      urlConn.setReadTimeout(60 * 1000); 
     } 
     if (urlConn != null && urlConn.getInputStream() != null) { 
      in = new InputStreamReader(urlConn.getInputStream(), 
        Charset.defaultCharset()); 
      BufferedReader bufferedReader = new BufferedReader(in); 
      if (bufferedReader != null) { 
       int cp; 
       while ((cp = bufferedReader.read()) != -1) { 
        sb.append((char) cp); 
       } 
       bufferedReader.close(); 
      } 
     } 
     in.close(); 
     return sb.toString(); 
    } 

    public JsonObject buildBook() { 
     JsonObject value = Json.createObjectBuilder() 
       .add("firstName", "John") 
       .add("lastName", "Smith") 
       .add("age", 25) 
       .add("address", Json.createObjectBuilder() 
         .add("streetAddress", "21 2nd Street") 
         .add("city", "New York") 
         .add("state", "NY") 
         .add("postalCode", "10021")) 
       .add("phoneNumber", Json.createArrayBuilder() 
         .add(Json.createObjectBuilder() 
           .add("type", "home") 
           .add("number", "212 555-1234")) 
         .add(Json.createObjectBuilder() 
           .add("type", "fax") 
           .add("number", "646 555-4567"))) 
       .build(); 
     return value; 
    } 

} 

試圖獲得JSON對象本頁面:

http://www.javascriptkit.com/dhtmltutors/javascriptkit.json

再次

,再次重申,強調的是,只用javax包。

回答

1

你可以做這樣的事情,雖然我從來沒有在javax中使用過Json API。

try(InputStream is = new URL(your_url).openStream(); 
JsonReader reader = Json. createReader(new InputStreamReader(is, "UTF-8"));){ 
//TODO: Let's start doing Yoga with Json 
} 
catch(IOException e){ 
e.printStackTrace(); 
} 
+0

http://docs.oracle.com/javaee/7/api/javax/json/JsonReader.html?唯一的原因是我從來沒有接受過你的回答,那就是我一直懶得去試試它......哈哈。 – Thufir