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?
echo json_encode([$value1, $value2]);. What you have at the moment creates an invalid JSON string.value1andvalue2are the same structure, then build a normal array andjson_encodethat. If they have different structures, then you probably want to build an associative array (i.e. a dictionary) andjson_encodethat.[ ]) 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