2017-03-13 1 views
0

나는 SwiftyJSON을 사용하고 있습니다.구문 분석 방법 json - SwiftyJSON

{ 
    "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|[email protected][email protected]" 
        }, 
        "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{[email protected][[email protected]" 
        }, 
        "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}[email protected]}@" 
        }, 
        "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|[email protected][}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 제가 싶어입니다 :

는이 같은 JSON 문자열이 있습니다.

내 코드 :

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.

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)") 
     } 
    } 
} 

추신을 :

+0

사전에서 배열을 구별해야합니다. 사전에 표시되지 않습니다. –

+2

'[ "routes"] [0] [ "legs"] [0] [ "거리"] [ "텍스트"]. 문자열'; routes는 배열이고 다리도 그렇습니다. –

답변

2

routeslegs 먼저 첫 번째 항목을 얻을 수 있고, 배열이다 배열에 첫 번째 항목을 원하지 않으면 논리 검사를해야 할 수도 있습니다.