簡體   English   中英

如何使用Alamofire swift 3從此JSON獲取值

[英]How to get values from this JSON using Alamofire swift 3

我正在嘗試從此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
  } ]

例如,我想獲取“配方”中的標簽值。 我已在控制台中成功打印了所有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]。 然后您的提示級別是一個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{

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM