如何获取解析 json - SwiftyJSON

Posted

技术标签:

【中文标题】如何获取解析 json - SwiftyJSON【英文标题】:How to get parse json - SwiftyJSON 【发布时间】:2017-03-13 04:48:03 【问题描述】:

我正在使用SwiftyJSON

我有一个这样的 json 字符串:


   "geocoded_waypoints" : [
      
         "geocoder_status" : "OK",
         "place_id" : "ChIJkcemZ9r9jT8RCoy7cipVk3Q",
         "types" : [ "route" ]
      ,
      
         "geocoder_status" : "OK",
         "place_id" : "ChIJzYyVGtcFjj8RMoV0IaoNhzA",
         "types" : [ "route" ]
      
   ],
   "routes" : [
      
         "bounds" : 
            "northeast" : 
               "lat" : 35.7884236,
               "lng" : 51.43490389999999
            ,
            "southwest" : 
               "lat" : 35.7056256,
               "lng" : 51.3073962
            
         ,
         "copyrights" : "Map data ©2017 Google",
         "legs" : [
            
               "distance" : 
                  "text" : "20.9 km",
                  "value" : 20893
               ,
               "duration" : 
                  "text" : "32 mins",
                  "value" : 1936
               ,
               "end_address" : "Tehran Province, Tehran, Hossein, Iran",
               "end_location" : 
                  "lat" : 35.7883586,
                  "lng" : 51.43490389999999
               ,
               "start_address" : "Tehran Province, Tehran, Kooy-e-Bimeh, Rostam, Iran",
               "start_location" : 
                  "lat" : 35.7056307,
                  "lng" : 51.3167437
               ,
               "steps" : [
                  
                     "distance" : 
                        "text" : "59 m",
                        "value" : 59
                     ,
                     "duration" : 
                        "text" : "1 min",
                        "value" : 21
                     ,
                     "end_location" : 
                        "lat" : 35.70562779999999,
                        "lng" : 51.31739839999999
                     ,
                     "html_instructions" : "Head \u003cb\u003eeast\u003c/b\u003e on \u003cb\u003eRostam\u003c/b\u003e toward \u003cb\u003e4th Bimeh St\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Burgerich (on the right)\u003c/div\u003e",
                     "polyline" : 
                        "points" : "ew|xEsxexH?W?w@?s@"
                     ,
                     "start_location" : 
                        "lat" : 35.7056307,
                        "lng" : 51.3167437
                     ,
                     "travel_mode" : "DRIVING"
                  ,



                  
                     "distance" : 
                        "text" : "0.1 km",
                        "value" : 135
                     ,
                     "duration" : 
                        "text" : "1 min",
                        "value" : 30
                     ,
                     "end_location" : 
                        "lat" : 35.7868183,
                        "lng" : 51.4303372
                     ,
                     "html_instructions" : "Continue onto \u003cb\u003eSharifi Manesh St\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Embassy of Switzerland In Tehran (on the left)\u003c/div\u003e",
                     "polyline" : 
                        "points" : "cllyEyyxHSIQSIIYWk@[_Ak@YQ"
                     ,
                     "start_location" : 
                        "lat" : 35.7857769,
                        "lng" : 51.4295729
                     ,
                     "travel_mode" : "DRIVING"
                  ,
                  
                     "distance" : 
                        "text" : "0.3 km",
                        "value" : 304
                     ,
                     "duration" : 
                        "text" : "1 min",
                        "value" : 51
                     ,
                     "end_location" : 
                        "lat" : 35.7852475,
                        "lng" : 51.4330904
                     ,
                     "html_instructions" : "\u003cb\u003eSharifi Manesh St\u003c/b\u003e turns \u003cb\u003eright\u003c/b\u003e and becomes \u003cb\u003eElahiyeh St\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by باشگاه الهیه (on the left)\u003c/div\u003e",
                     "polyline" : 
                        "points" : "srlyEs~xHpBwEdABxAqCf@@"
                     ,
                     "start_location" : 
                        "lat" : 35.7868183,
                        "lng" : 51.4303372
                     ,
                     "travel_mode" : "DRIVING"
                  ,
                  
                     "distance" : 
                        "text" : "0.4 km",
                        "value" : 359
                     ,
                     "duration" : 
                        "text" : "1 min",
                        "value" : 70
                     ,
                     "end_location" : 
                        "lat" : 35.7884241,
                        "lng" : 51.4337388
                     ,
                     "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eKhazar St\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by MaryamHair (on the left)\u003c/div\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : 
                        "points" : "yhlyEyo|xHwBYwCg@wA[AQuAMAC"
                     ,
                     "start_location" : 
                        "lat" : 35.7852475,
                        "lng" : 51.4330904
                     ,
                     "travel_mode" : "DRIVING"
                  ,
                  
                     "distance" : 
                        "text" : "0.1 km",
                        "value" : 105
                     ,
                     "duration" : 
                        "text" : "1 min",
                        "value" : 39
                     ,
                     "end_location" : 
                        "lat" : 35.7883586,
                        "lng" : 51.43490389999999
                     ,
                     "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e at هنرستان مفتح onto \u003cb\u003eHossein\u003c/b\u003e",
                     "maneuver" : "turn-right",
                     "polyline" : 
                        "points" : "s|lyEs|xH?AJeF"
                     ,
                     "start_location" : 
                        "lat" : 35.7884241,
                        "lng" : 51.4337388
                     ,
                     "travel_mode" : "DRIVING"
                  
               ],
               "traffic_speed_entry" : [],
               "via_waypoint" : []
            
         ],
         "overview_polyline" : 
            "points" : "ew|xEsxexH?oA"
         ,
         "summary" : "Hakim Expy",
         "warnings" : [],
         "waypoint_order" : []
      
   ],
   "status" : "OK"

我想得到的是routes->legs->distance->text

我的代码:

   let json = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String
        if let data = json.data(using: String.Encoding.utf8) 
            let json = JSON(data: data)

            print("--->\(json["routes"]["legs"]["distance"]["text"].string)")


        

print 返回--->nil

【问题讨论】:

您需要将数组与字典区分开来,我在您的尝试中根本没有看到。 ["routes"][0]["legs"][0]["distance"]["text"].string; routes 是一个数组,legs 也是。 【参考方案1】:

routeslegs 是数组,必须先获取第一个条目:

let json = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String
if let data = json.data(using: String.Encoding.utf8) 
    let json = JSON(data: data)
    if let route = json["routes"].first 
        if let leg = route["legs"].first 
            print("--->\(leg["distance"]["text"].string)")
        
    

附言如果您不想要数组中的第一个条目,您可能需要进行一些逻辑检查。

【讨论】:

以上是关于如何获取解析 json - SwiftyJSON的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 SwiftyJSON 在 Swift 中解析这个 JSON 对象?

如何使用 Alamofire 和 SwiftyJSON 正确解析 JSON

如何使用 SwiftyJSON 解析特定格式的 json?

使用 swiftyjson 和 swift 解析嵌入的 json

使用 SwiftyJSON 解析 JSON

使用 SwiftyJSON iOS 解析 JSON 响应