2017-11-18 6 views
-3

次のリンクからデータを解析しようとしています:https://feeds.divvybikes.com/stations/stations.jsonjsonでDivvy自転車データを解析する際のトラブル

 func getData() { 

     let url = URL(string:"https://feeds.divvybikes.com/stations/stations.json") 

     // Submit a request 
     let task = URLSession.shared.dataTask(with: url!) { (data,response,error) in 

      // print error if there is one 
      if error != nil { 
       print(error!) 
       self.alert(messageTitle: "Error", messageM: "An error occured") 
       return 
      } // end if 

      // if there is no error, fetch the json 
      if let content = data { 
       do { 

        let json = try JSONSerialization.jsonObject(with: content, options:JSONSerialization.ReadingOptions.mutableContainers) as AnyObject 

        if let stationName = json["stationBeanList"] as? [String: AnyObject] { 
         for station in stationName{ 
          if let lat1 = ["latitude"] as? CGFloat, let long1 = ["longitude"] as? CGFloat{ 


           print(lat1,long1) 
          } 
         } 


         } 

       } 








       catch { 
        print(error) 
        self.alert(messageTitle: "Error", messageM: "An error occured") 
       } 
      }//end if 
    }//end task 
     task.resume() 

//endqueue 
} 

私は上記のリンクから経度と緯度を取得し、迅速に埋め込むのMapView上にプロットしたいと思いますが、私はプリントアウトしたり、適切に保存するためにも、緯度と経度を取得するように見える傾けます。どんな助けでも大歓迎です。

i)が (負荷のgetData関数を呼び出すか、私はアプリケーションを実行すると何もデバッガで現れていません。私が見つけたものを、あなたのコードを通過した後

+1

あなたの質問では、スクリーンショットのようにコードを含めないでください。それをテキストとして質問にコピーし、適切なコードフォーマットを使用します。タイプ(キー:文字列値:ANYOBJECTを)私は私のせい –

+0

右手は –

答えて

0

は、Webサービスの呼び出しからLAT-長い取得するにはちょうどマイナーなミスを犯しています。

あなたが値を取得したい場合は、stationNamefor loop後にこれに応じて移動する必要があります。 ここでは、より明確にするためのサンプルスニペットを示します。

for station in stationName{ 
    print(station["latitude"] as! Double) 
    print(station["longitude"] as! Double) 
} 

希望これはあなたを助けます:)

+0

はちょうどそれを試してみましたというエラーを持っているコードを追加している必要があります –

0

をコードで文字列として緯度と経度を検討しています。しかし、その値はString型ではありません。

したがってprint(lat1,long1)は決して呼び出されません。

ソリューション:多くの問題があります

 // Submit a request to get the JASON data 
     let task = URLSession.shared.dataTask(with: url!) { (data,response,error) in 

      // if there is an error, print the error and do not continue 
      if error != nil { 
       print(error!) 
       self.alert(messageTitle: "Error", messageM: "An error occured") 
       return 
      } // end if 

      // if there is no error, fetch the json formatted content 
      if let content = data { 
       do { 

        let jsonObject = try JSONSerialization.jsonObject(with: content, options: []) as AnyObject 

        if let JSON = jsonObject["stationBeanList"] as? [[String:AnyObject]] { 
         for stationInfo in JSON { 

          if let longitude = stationInfo["longitude"] as? Float64, let latitude = stationInfo["latitude"] as? Float64         { 
           print(longitude,latitude) 



            //  self.objects.append(stationInfo(latitude:latitude,longitude:longitude)) 
          }//end if 
         }//end if let 
        }//end do 


       }//end do 
       catch { 
        print(error) 
        self.alert(messageTitle: "Error", messageM: "An error occured") 
       } 
      }//end if 
     }//end task 
     task.resume() 
    }//endqueue 
} 
+0

はちょうどそれは私がちょうどコードが含まれて機能しなかったことを試してみました何の添字memebrsを持っていません私のせいである画像の代わりに –

0

のlet URL = URL( "https://feeds.divvybikes.com/stations/stations.json" の文字列)だと思う動作します。

  • 主な問題は、stationBeanListの値がアレイではない辞書ということです。
  • .mutableContainersは、Swiftではまったく役に立たず、パラメータを省略します。
  • スウィフト3+ JSON辞書アレイが[[String:Any]]で、[String:Any]あります。
  • スウィフト3+で、不特定のJSON型はAny、決してAnyObjectです。
  • 標準のJSON浮動小数点値はDoubleです。
  • 予想されるタイプがより具体的な場合は、JSON結果をAnyにキャストしないでください。

if let json = try JSONSerialization.jsonObject(with: content) as? [String: Any] { 
    if let stationBeanList = json["stationBeanList"] as? [[String: Any]] { 
     for stationInfo in stationBeanList { 
      let lat = stationInfo["latitude"] as! Double 
      let lng = stationInfo["longitude"] as! Double 
      print(lat, lng) 
     } 
    } 
} 
+0

私はそれをfloat64にして、経度と緯度を初期化するクラスを作成しなければならなかった –

0

:CGFloatの代わりに、文字列としてそれを考えてみましょう、それは私がそう