【问题标题】:Parsing JSON string in Swift 2在 Swift 2 中解析 JSON 字符串
【发布时间】:2015-09-27 19:59:14
【问题描述】:

我正在用 Swift 做一个项目(确切地说只是学习 Swift)

并且必须提取并显示 API 的 JSON 输出。

这里是 API 规范/示例

API Documentation https://developer.nutritionix.com/v1_1/quick-start/upc-scan

这是目前为止的代码 -

let url = "https://api.nutritionix.com/v1_1/item?upc="

        let urlWithUPC = url + barcode + "&appId=[YOURID]&appKey=[YOURKEY]"

        print("API Query: "+urlWithUPC)

        NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: urlWithUPC)!) { data, response, error in
            // Handle result
            print("Checked the bar code")

            let itemDescription = (((data as NSArray)[0] as? NSDictionary)?["item_description"] as? NSString)
            print("Item Description :" + itemDescription)
            }.resume()

现在,我只想扫描 UPC(有效),查找项目(有效),取回数据(有效),然后提取并显示所需的数据部分(无效) .

任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • data 是一个 NSData 对象(原始字节),您必须使用 NSJSONSerialization 对其进行反序列化
  • NSJSONSerialization 解决了这个问题。谢谢。

标签: ios json swift api


【解决方案1】:

您还没有将AppIDSecret 放入网址中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    相关资源
    最近更新 更多