为什么我的JSON URL不输出JSON数据?

时间:2016-02-20 10:21:54

标签: ios json swift

我检查了网址并且它有效,但是当我尝试输出readableJSON时,它是空的?!

这是我的输出:

Not empty now
2016-02-20 10:19:29.162 ThisLondon[55922:4080604] the URL: [

]
2016-02-20 10:19:29.162 ThisLondon[55922:4080604] 0

这是我的代码:

selectedTube = "Circle"
let url = NSURL(string: "https://api.tfl.gov.uk/Line/\(selectedTube)/Arrivals?app_id=6573044a&app_key=7a97df35339dc68625384077a5e07304")
let jsonData = NSData(contentsOfURL: url!)
let readableJSON = JSON(data: jsonData!, options: NSJSONReadingOptions.MutableContainers, error: nil)

NSLog("the URL: \(readableJSON)")

2 个答案:

答案 0 :(得分:1)

尝试使用:

import java.sql.*;

public  class DbConnector {

   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_ADDRESS = "jdbc:mysql://localhost:8889/java_prog";

   String query;

     static final String USER = "root";
     static final String PASS = "root";





  public static String dbAsker (String query){
    Connection conn = null;
    Statement stmt = null;
    String return_statement;

     try{
      //STEP 2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");

      //STEP 3: Open a connection
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_ADDRESS,USER,PASS);

      //STEP 4: Execute a query

      ResultSet rs = stmt.executeQuery(query);



     return_statement = rs.toString();

      //STEP 6: Clean-up environment
      rs.close();
       stmt.close();
      conn.close();

   }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
      e.printStackTrace();
   }finally{
      //finally block used to close resources
       try{
         if(stmt!=null)
            stmt.close();
      }catch(SQLException se2){
      }// nothing we can do
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end finally try
   }//end try
   System.out.println("Goodbye!");

   return return_statement;



  }
} 

答案 1 :(得分:0)

我不太清楚你的问题是什么。以下代码在游乐场中运行良好。我怀疑问题出在您的自定义JSON函数中:

import Foundation

let selectedTube = "Circle"
let url = NSURL(string: "https://api.tfl.gov.uk/Line/\(selectedTube)/Arrivals?app_id=6573044a&app_key=7a97df35339dc68625384077a5e07304")
if let jsonData = NSData(contentsOfURL: url!) {
    let readableJSON = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers)
    NSLog("the URL: \(readableJSON)")
} else {
        print("Cannot read data.")
}