如何从数组中获取JSON格式? - 迅速

时间:2016-10-21 11:28:15

标签: ios arrays json swift xcode

如何从数组中获取JSON格式?这是我的JSON格式化程序

{
  "productName": "Lemonade",
  "transaction": ["[2016-08-01 10:23:42] - Get product in warehouse",
                "[2016-08-01 10:53:22] - Sent to customer"]
}

我的代码有错误。我不知道如何从事务数组中获取JSON格式。

if let jsonObject = try JSONSerialization.jsonObject(with: returnData, options: .allowFragments) as? [String: AnyObject]
                    {
                        valSucceeded = (jsonObject["succeeded"] as? Bool)!;
                        valResponseCode = (jsonObject["responseCode"] as? String)!;
                        valResponseDescription = (jsonObject["responseDescription"] as? String)!;
                        valSerialNumber = (jsonObject["serialNumber"] as? String)!;
                        valProductName = (jsonObject["productName"] as? String)!;
                        if let transactionsArray = jsonObject["transactions"] as? Array<AnyObject> {

                            for transact in transactionsArray
                            {
                                if let transactionStr = transact["transactions"] as? String {
                                    valTransactions = transactionStr
                                } else {
                                    valTransactions = ""
                                }
                            }
                        }

                        let entryResult = TraceModel(succeeded: valSucceeded,
                                                     responseCode: valResponseCode,
                                                     responseDescription: valResponseDescription,
                                                     serialNumber: valSerialNumber,
                                                     productName: valProductName,
                                                     transactions: valTransactions);
                        traceArray.append(entryResult);
                    }

0 个答案:

没有答案