如何在查询中合并 2 个 JSON

how to combine 2 json in query

提问人:grooot 提问时间:2/18/2021 最后编辑:grooot 更新时间:2/19/2021 访问量:31

问:

我需要附加 2 个 JSON。
目前,它打印为两个单独的 Json。
我如何结合我的两种阅读方式????我希望我的 Json 将两个读数放在一起。

let query = HKSampleQuery(sampleType: glucoseType!, predicate: predicate, limit: 5, sortDescriptors: nil) { (query, results, error) in
                for result in results as! [HKQuantitySample] {
                  let bloodGlucose = result.quantity.description
                  let bloodGlucoseItem = HealthDataItem(endDate: result.endDate, value: String(bloodGlucose), startDate: result.endDate, type: .bloodGlucose)
                          let healthData = [bloodGlucoseItem]
                          do {
                            let data = try JSONEncoder().encode(healthData)
                            let json = String(data: data, encoding: String.Encoding.utf8)
                            for i in 0..<results!.count {
                              if (i != 0){
                                json!.append(json!)
                              }
                              print("json data:", json as Any )
                            }
                            resolve(json)
                          } catch {
                               //error handling
                          }
                }
            }


安慰:

json data: Optional("[{\"value\":\"8 mg\\/dL\",\"endDate\":635390040,\"startDate\":635390040,\"type\":\"bloodGlucose\"}]")

json data: Optional("[{\"value\":\"8 mg\\/dL\",\"endDate\":635390040,\"startDate\":635390040,\"type\":\"bloodGlucose\"}]
[{\"value\":\"8 mg\\/dL\",\"endDate\":635390040,\"startDate\":635390040,\"type\":\"bloodGlucose\"}]")

json data: Optional("[{\"value\":\"6 mg\\/dL\",\"endDate\":635393640,\"startDate\":635393640,\"type\":\"bloodGlucose\"}]")

json data: Optional("[{\"value\":\"6 mg\\/dL\",\"endDate\":635393640,\"startDate\":635393640,\"type\":\"bloodGlucose\"}]
[{\"value\":\"6 mg\\/dL\",\"endDate\":635393640,\"startDate\":635393640,\"type\":\"bloodGlucose\"}]")

iOS JSON Objective-C Swift Xcode

评论

0赞 Larme 2/18/2021
你有两个数组(在示例中字符串化;但我想这已经是一个数组了),只是?并使用 DispatchGroup,如果它们是异步的。healdDataappend(contentsOf:)enter()leave()notify()
0赞 grooot 2/19/2021
我尝试使用append方法。它再次打印相同的数据。(查看我更新的问题)

答: 暂无答案