0

Im working on a project in swift 3 and to call web services I use AlamoreFire and SwiftyJASON (pods). The Format as Bellow

    APIManager.apiGet(serviceName:appURLs.mainBaseURL+appURLs.testingPureLight, parameters: params) { (json:JSON?, error:NSError?) in
            if error != nil{

                print("error:",error as Any)

            }

            print(json!)
}

Therefore, I'm getting a Json response with type of JSON, thus,I get the following structure as my response.

 { 
  "media": [ {
    "key" : "value" 
    "Objectarray"[
     { "Object1":"somevalue" 
          "key2":"somevalue"
          "key3":"somevalue"
     } 

     {"Object2":"somevalue"
         "key2":"somevalue"
         "key3":"somevalue"
     } 

     { "Object3":"somevalue" 
          "key2":"somevalue"
          "key3":"somevalue"
     }

    ] 
  } ]
 }

My requirement is to catch this "Objectarray" and assign it to a string array so I could use it to populate in my UITableVIew. How could I achieve this ?

4
  • Is Objectarray contains key like Object1, Object2, Object3... or you have written like that. Commented Mar 10, 2017 at 10:20
  • Object array have Dictionary elements called Object1, Object2, Object3, and inside that only u get key value pairs. For instance inside Object1 may contains a pair and it could be like-> Object1{"name" : "john"} Commented Mar 11, 2017 at 9:45
  • What I'm asking is Object1, Object2, Object3... are actual keys? Commented Mar 11, 2017 at 9:48
  • yes they are keys but inside separate dictionaries, Ill update the code to make it more sense Commented Mar 12, 2017 at 6:22

1 Answer 1

1

You can get your Objectarray this way.

var objects = [String]() 
if let jsonResponse = json,
   let objectArray = jsonResponse["media"][0]["Objectarray"].arrayValue {

    for object in objectArray {
        let object1 = object["Object1"].stringValue
        objects.append(object1)
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

no sir as i have mentioned what i wants to know is how could i assign it to a string array and use it in my cellForRow method. With what u have written above, the type of object array is JSON. Not String
some bracket is missing i guess.Something wrong its not working. so many errors but got the basic idea though
@danutha Then you need to Proper your code and check where the bracket missing because in my answer there is no missing bracket, all the opening brackets have there closing bracket.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.