1

I wanted to get a multiple jsonObject from PHP to my application. The problem is I can't seem to get more than one object to my app. Below is an example of what I wanted to do:

PHP file :

<?php 

   echo json_encode([$value1, $value2]);

?>

I used the dataTask method that looks like below to get the data from the PHP to my Swift application.

let task = URLSession.shared.dataTask(with: request) { (data: [Data?], 
response: URLResponse?, error: Error?) in

 var result = [Conversation]()
 var chck = [String]()  

 do{
     result = try JSONDecoder().decode([Conversation].self, from: data![0]) as Data)
     chck = try JSONDecoder().decode([String].self, from: data![1]) as Data)

  }catch{
       print("error"+error.localizedDescription)

}
        //do something
}
task.resume()

Obviously, I didn't get both of the variable. I tried to put $value1 and $value2 in an array and then echo it to the application, but apparently it didn't work for me. data![0] and data![1] gives error. Could anyone lend me a hand please?

3
  • 4
    Send it as an array e.g. echo json_encode([$value1, $value2]);. What you have at the moment creates an invalid JSON string. Commented Dec 22, 2017 at 15:42
  • 3
    "I tried to put $value1 and $value2 in an array ... but ... it didn't work" ... Show us what you tried. That's the right basic idea. If value1 and value2 are the same structure, then build a normal array and json_encode that. If they have different structures, then you probably want to build an associative array (i.e. a dictionary) and json_encode that. Commented Dec 22, 2017 at 15:49
  • actually I tried to do as what Jon showed above (but i didn't put [ ] ) somehow I think the problem comes from the Swift side, I just dont know how to decode the object.. I'll show you what i did, I'll edit the question @Rob Commented Dec 22, 2017 at 16:01

1 Answer 1

2

I'm inferring from your Swift code that $value1 and $value2 are two different types of results. For collections of heterogenous objects, I'd suggest using an associative array (i.e., a dictionary):

<?php 

   echo json_encode(Array("result" => $value1, "chck" => $value2));

?>

And you'd parse that with:

struct ServerResult: Decodable {
    let result: [Conversation]
    let chck: [String]
}

And

let serverResult = try JSONDecoder().decode(ServerResult.self, from: data!)

Now, given that you haven't shared the details, I'm assuming that:

  • $value1 is a properly structured array of Conversation;
  • Conversation is, itself, Decodable; and
  • $value2 is a properly structured array of String.

I'd also suggest avoiding data!, because your app will just crash if there is some server error. I'd suggest you gracefully detect errors, e.g.:

let task = URLSession.shared.dataTask(with: request) { data, _, error in    
    guard let data = data, error == nil else {
        print(error ?? "Unknown error")
        return
    }

    do {
        let serverResult = try JSONDecoder().decode(ServerResult.self, from: data)
        // do something
    } catch let parseError {
        print(parseError)
    }
}
task.resume()
Sign up to request clarification or add additional context in comments.

1 Comment

That is exactly what I'm having, your assumption is 100% correct!...Thanks for the help , appreciate it

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.