SwiftyJson get value of json from complicated structure of JSON

568 Views Asked by At

This my json.file:

[
{
"date_range": "2016-11-01-2016-12-31",
"order_status_id": 3,
"jobs": [
  {
    "date": "2016-11-14",
    "job": [
      {
        "id": 143608,
        "pickup_worker_id": null,
        "drop_off_worker_id": 57
      }
           ]
   }
       ]
 }

 {
    "date_range": "2016-11-01-2016-12-31",
    "order_status_id": 2,
    "jobs": [
       {
          "date": "2016-11-16",
          "job": [
             {
                "id": 143238,
                "pickup_worker_id": null,
                "drop_off_worker_id": 12
              }, 
            {
                "id": 13218,
                "pickup_worker_id": null,
                "drop_off_worker_id": 42
              } 
                 ]
       },
       {
          "date": "2016-11-19",
          "job": [
             {
                "id": 141238,
                "pickup_worker_id": null,
                "drop_off_worker_id": 12
              } 

                 ]
           }


             ]
   }
]

This my code for swiftyjson:

 Alamofire.request(Constants.web_api+api_get_orders, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: Constants.headers ).responseJSON { (responseData) -> Void in
        if((responseData.result.value) != nil) {
            let swiftyJsonVar = JSON(responseData.result.value!)
            print("All Data JSON \(swiftyJsonVar)")

          print("date range1\(swiftyJsonVar["date_range"])")
          print("date range2\(swiftyJsonVar["date_range"].stringValue)")
            print("jobs1 \(swiftyJsonVar["jobs"].arrayObject)")
            print("jobs2 \(swiftyJsonVar["jobs"].array)")
            print("jobs3 \(swiftyJsonVar["jobs"])")
            print("jobs date \(swiftyJsonVar["jobs"]["date"].stringValue)")
            print("jobs date \(swiftyJsonVar["jobs"]["job"]["drop_off_worker_id"].stringValue)")

        }

The output , all is null or nil except All Data JSON (swiftyJsonVar). How can I get value of date_range, drop_off_worker_id ? I really hope someone can help me. I spend a lot of time for solve it but still can't solve it.

2

There are 2 best solutions below

9
On BEST ANSWER

Your JSON response is Array not Dictionary, so you need to access its first object to get the detail you want.

if let dateRange = swiftyJsonVar[0]["date_range"].string  {
    print(dateRange)
}
if let worker_id = swiftyJsonVar[0]["jobs"][0]["job"][0]["drop_off_worker_id"].int {
    print(worker_id)
}

Edit: If you have multiple object in your root than get all the dateRange and worker_id in for loop.

for subJson in swiftyJsonVar.array {

    if let dateRange = subJson["date_range"].string  {
         print(dateRange)
    }
    for jobsJson in subJson["jobs"].array {
         for jobJson in jobsJson["job"].array {
              if let worker_id = jobJson["drop_off_worker_id"].int {
                   print(worker_id) 
              }
         }
    }
}
2
On

Please try, by referring the index of the element in the array.

Alamofire.request(Constants.web_api+api_get_orders, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: Constants.headers)
  .responseJSON { (responseData) -> Void in
  if((responseData.result.value) != nil) {
    let swiftyJsonVar = JSON(responseData.result.value!)
    print("All Data JSON \(swiftyJsonVar)")

    print("date range1\(swiftyJsonVar[0]["date_range"])")
    print("date range2\(swiftyJsonVar[0]["date_range"].stringValue)")
    print("jobs1 \(swiftyJsonVar[0]["jobs"].arrayObject)")
    print("jobs2 \(swiftyJsonVar[0]["jobs"].array)")
    print("jobs3 \(swiftyJsonVar[0]["jobs"])")
    print("jobs date \(swiftyJsonVar[0]["jobs"]["date"].stringValue)")
    print("jobs date \(swiftyJsonVar[0]["jobs"]["job"]["drop_off_worker_id"].stringValue)")
}