Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用字符串获取Json子级_Json_Swift_String - Fatal编程技术网

使用字符串获取Json子级

使用字符串获取Json子级,json,swift,string,Json,Swift,String,我不想获取名为symbolString的字符串的值。我不知道怎么做。这就是我目前得到的。我首先尝试获取数据子项,然后转到symbolString获取ImageUrl值。它似乎不起作用。请帮忙 final let urlString = "https://www.cryptocompare.com/api/data/coinlist/" func downloadJsonWithURL() { let url = NSURL(string: urlStri

我不想获取名为
symbolString
的字符串的值。我不知道怎么做。这就是我目前得到的。我首先尝试获取数据子项,然后转到
symbolString
获取ImageUrl值。它似乎不起作用。请帮忙

final let urlString = "https://www.cryptocompare.com/api/data/coinlist/"

        func downloadJsonWithURL() {
            let url = NSURL(string: urlString)

            URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) -> Void in
                if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
                    if let DataArray = jsonObj!.value(forKey: "Data") as? NSArray {
                        print(DataArray)
                        for Data in DataArray{
                            if let DataDict = Data as? NSDictionary {
                                if let nameURL = DataDict.value(forKey: "\(self.symbolString)") as? NSDictionary {
                                    if let name = nameURL.value(forKey: "ImageUrl") {
                                        self.imageString.append(name as! String)
                                        print(self.imageString)
                                    }
                                }
                            }
                        }
                    }
                }
            }).resume()
        }

您可以尝试使用以下代码。这对我有用

 func downloadJsonWithURL() {
    let url = "https://www.cryptocompare.com/api/data/coinlist/"
    var urlRequest = URLRequest(url: URL(string: url)!)
    let task = URLSession.shared.dataTask(with: urlRequest) { data, response, error in

        guard let unwrappedDAta = data else {
            print("Error unwrapping data")
            return
        }

        do {
            let responseJSON = try JSONSerialization.jsonObject(with: unwrappedDAta, options: []) as? NSDictionary

            if let dataDict = responseJSON!["Data"] as? NSDictionary {
                for (key, _) in dataDict {
                    if let keyDict = dataDict[key] as? NSDictionary {
                        if let imageURL = keyDict["ImageUrl"] as? String {
                            self.imageString.append(imageURL)
                        }
                        print(self.imageString)
                    }
                }
            }
        } catch {
            print("Could not get API data. \(error))")
        }
    }
    task.resume()
}

JSON中没有数组(
[]
)。所有集合类型都是字典(
{}
)。顺便说一句,变量名应该以小写字母开头,并且在Swift中不使用
NSArray、NSDictionary
valueForKey
,数据中似乎没有任何
symbolString
的实例。您希望在哪里找到它?symbolString是一个字符串,例如“BTC”。行了,我检查过了。但是如果你看url,我首先需要在“数据”中找到,而不是像“BTC”(符号字符串)。但我不知道怎么做。