我想像这样将数组转换为字符串
$stations_id = Station::pluck('id'); // Output [1,2,3,4]
$schedules = [];
foreach ($stations_id as $station_id) {
$schedules[] = Schedule::select('id')
->where('station_id', $station_id)
->latest()
->first();
}
// second loop here
$queues = [];
foreach ($schedules as $schedule) {
$queues[] = Queue::withTrashed()
->latest()
->where('schedule_id', $schedule->getKey())->first();
}
// do something with queues
到
let myArray:[Any] = ["element 1", "element 2", ["element 3.1", "element 3.2"], "element 4"]
我已经尝试过了
let convertedString = "[\"element 1\",\"element 2\",\"[\\\"element 3.1\\\",\\\"element 3.2\\\"]\",\"element 4\"]"
但是我得到这个结果
do {
let jsonData: Data = try JSONSerialization.data(withJSONObject: myArray, options: [])
if let jsonString = NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue) {
print(jsonString)
}
} catch let error as NSError {
print("Array convertIntoJSON - \(error.description)")
}
我已经使用JSONArray在Java中完成了此操作。我只是调用toString方法在Java中执行此操作。我需要像上面给出的示例一样将第三个元素作为字符串。
答案 0 :(得分:2)
这应该做:
"\(myArray.map { "\($0)" })"