ios - 如何使用 Alamofire swift 3 从这个 JSON 中获取值

标签 ios json swift alamofire

我正在尝试从此 JSON 中获取值。这是 JSON 的一部分,因为真正的 JSON 数据太长。

"hits" : [ {
"recipe" : {
  "uri" : "http://www.edamam.com/ontologies/edamam.owl#recipe_bd8def1d09d8c308f659e6945f366271",
  "label" : "Pinchos de carne",
  "image" : "http://img.recetascomidas.com/recetas/640_480/pinchos-de-carne.jpg",
  "source" : "Recetal Comidas",
  "url" : "http://pinchos-de-carne.recetascomidas.com/",
  "shareAs" : "http://www.edamam.com/recipe/pinchos-de-carne-bd8def1d09d8c308f659e6945f366271/carne",
  "yield" : 6.0,
  "dietLabels" : [ "Low-Carb" ],
  "healthLabels" : [ "Dairy-Free", "Gluten-Free", "Egg-Free", "Peanut-Free", "Tree-Nut-Free", "Soy-Free", "Fish-Free", "Shellfish-Free" ],
  "cautions" : [ ],
  "ingredientLines" : [ "600 gr. de carne magra de cerdo", "2 cucharadas de pimentón dulce", "1 cucharada de pimentón picante", "1 cucharada de cúrcuma", "1 cucharada de sazonador especial para carnes", "3 cucharadas de perejil fresco bien picadito", "1/2 cucharada de comino", "Aceite de oliva", "3 patatas", "1 diente de ajo", "Pimienta negra", "Sal" ],
  "ingredients" : [ {
    "text" : "600 gr. de carne magra de cerdo",
    "quantity" : 600.0,
    "measure" : null,
    "food" : "carne magra cerdo",
    "weight" : 600.0
  }, {
    "text" : "2 cucharadas de pimentón dulce",
    "quantity" : 2.0,
    "measure" : "tbsp",
    "food" : "pimenton dulce",
    "weight" : 13.6
  }, {
    "text" : "1 cucharada de pimentón picante",
    "quantity" : 1.0,
    "measure" : "tbsp",
    "food" : "pimenton picante",
    "weight" : 6.8
  }, {
    "text" : "1 cucharada de cúrcuma",
    "quantity" : 1.0,
    "measure" : "tbsp",
    "food" : "curcuma",
    "weight" : 6.8
  }, {
    "text" : "1 cucharada de sazonador especial para carnes",
    "quantity" : 1.0,
    "measure" : "tbsp",
    "food" : "sazonador",
    "weight" : 2.7
  }, {
    "text" : "3 cucharadas de perejil fresco bien picadito",
    "quantity" : 3.0,
    "measure" : "tbsp",
    "food" : "perejil fresco",
    "weight" : 11.4
  }, {
    "text" : "1/2 cucharada de comino",
    "quantity" : 0.5,
    "measure" : "tbsp",
    "food" : "comino",
    "weight" : 3.0
  }, {
    "text" : "Aceite de oliva",
    "quantity" : 2.0,
    "measure" : "tbsp",
    "food" : "aceite oliva",
    "weight" : 27.0
  }, {
    "text" : "3 patatas",
    "quantity" : 3.0,
    "measure" : null,
    "food" : "patatas",
    "weight" : 195.0
  }, {
    "text" : "1 diente de ajo",
    "quantity" : 1.0,
    "measure" : null,
    "food" : "ajo",
    "weight" : 3.0
  }, {
    "text" : "Pimienta negra",
    "quantity" : 1.0,
    "measure" : "peppercorn",
    "food" : "pimienta negra",
    "weight" : 0.3
  }, {
    "text" : "Sal",
    "quantity" : 0.0,
    "measure" : null,
    "food" : "sal",
    "weight" : 3.3346887
  } ]

例如,我想获取“recipe”中的标签值。我在控制台中成功打印了所有 JSON,但我不知道如何获取配方值。

我希望你能帮助我。

这是我现在的代码。

let url: String = "https://test-es.edamam.com/search?q=pollo"
var arregloHits: NSMutableArray = []

override func viewDidLoad() {
    super.viewDidLoad()

    pruebaJson()
}

func pruebaJson(){
    if ControladorService.conexionInternet(){
        ControladorService.sharedInstance.callUrlWithCompletion(url: url, params: nil, completion: { (finished, response) in
            if finished{
                let result = NSMutableArray(array: response["hits"] as! NSArray)

                self.arregloHits = result
                print(self.arregloHits)
            }else{
                print("Connection failed")
            }
        }, method: .get)
    }else{
        print("No Internet")
    }
}

最佳答案

您的顶级 json 结构是一个字典。所以你需要把它解析成[String: Any]。那么你的hints level是一个JSON数组,所以你需要把它解析成[Any]

这是一个例子。不要忘记处理展开

do{
    let json = try JSONSerialization.jsonObject(with: yourJSONData, options: []) as? [String: Any]

    let hits = json?["hits"] as? [Any]
    dump(hits)
}catch let error{

}

关于ios - 如何使用 Alamofire swift 3 从这个 JSON 中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44991885/

相关文章:

关于电池电量的 iOS 通知

iphone - 在移动版 Safari 中仅缩放图像

javascript - Uncaught Error : Invalid type for google table column

python - 使用 python 读取包含 json 数据的文件会抛出一个我无法识别的错误

ios - 当用户在我的应用程序中滚动 UIViewCollectionCells 列表时出现奇怪的行为(每个单元格中的数据随机变化)

swift - 尝试创建一个 for 循环,根据倍数输出某些单词

ios - 如何使用 willDisplayCell 方法实现多个自定义单元格?

iOS:根据数组的顺序对文件夹中的文件进行排序

ios - 变换使 UIView 在 iPhone 6 设备上消失

java - 如何从 OpenAPI 3.0 yaml 文件生成 JSON 示例?