JSON Swift解包[""]

时间:2018-04-06 22:37:15

标签: json swift

我有一个问题是使用SwiftyJSON正确解开我的字符串

  

输出:[" AAPL"]

          stockData = try Data(contentsOf: url!)

            let json = try JSON(data: stockData)

 if let jsonArray = json["Stock Quotes"].array {

                let ticker = String(describing: jsonArray.map({$0["1. symbol"].stringValue}))

                print(ticker)



            }

JSON

抱歉,这是完整的JSON

  {
    "Meta Data": {
        "1. Information": "Batch Stock Market Quotes",
        "2. Notes": "IEX Real-Time Price provided for free by IEX (https://iextrading.com/developer/).",
        "3. Time Zone": "US/Eastern"
    },
    "Stock Quotes": [
        {
            "1. symbol": "AAPL",
            "2. price": "168.3500",
            "3. volume": "34941964",
            "4. timestamp": "2018-04-06 16:55:36"
        }
    ]
}

2 个答案:

答案 0 :(得分:0)

if let jsonArray = json["Stock Quotes"].array {
       if let ticker =  jsonArray[0]["1. symbol"] as? String {
           print(ticker)
       }
}

答案 1 :(得分:0)

你能试试吗

  if let jsonArray = json["Stock Quotes"].array {

           if let ticker =  jsonArray[0]["1. symbol"] as? String {

               print(ticker)

           }
   }