如何解析json - SwiftyJSON

时间:2017-03-13 04:48:04

标签: json swift swifty-json

我正在使用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" : "cllyEyy{xHSIQSIIYWk@[_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~{xHpBwEdA}BxAqCf@}@"
                     },
                     "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[}AQuAM}AC"
                     },
                     "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|lyE{s|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

1 个答案:

答案 0 :(得分:2)

CML_System_IO_CaptureStdOutput.clwroutes是数组,您必须先获得第一个条目:

legs

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