gpt4 book ai didi

ios - 无法返回正确的 JSON 值

转载 作者:行者123 更新时间:2023-11-28 11:16:54 25 4
gpt4 key购买 nike

我一直试图在处理 JSON 数据时返回我收到的数据中的特定值,但我似乎无法获得特定值。我只能得到所有的信息。这是更详细的问题:

代码:

class ViewController: UIViewController, NSURLConnectionDelegate {

lazy var data = NSMutableData()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
startConnection()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func startConnection(){
let urlPath: String = "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/soon2challenger?api_key=(removed my private API key for obvious reasons)"
var url: NSURL = NSURL(string: urlPath)!
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
connection.start()
}

func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
self.data.appendData(data)
}

func buttonAction(sender: UIButton!){
startConnection()
}

func connectionDidFinishLoading(connection: NSURLConnection!) {
var err: NSError
// throwing an error on the line below (can't figure out where the error message is)
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
println(jsonResult)
}
}

代码运行良好。这是我得到的结果。

{
soon2challenger = {
id = 43993167;
name = soon2challenger;
profileIconId = 844;
revisionDate = 1435549418000;
summonerLevel = 30;
};
}

问题是,当我想从列表中返回某个值时,如在 id 中,我似乎无法那样做。我尝试执行 println(jsonResult["id"]) 但这只是结果为零。

如何从列表中返回某个值?前任。 id, name, profileIconId

最佳答案

尝试像这样使用 objectForKey:

let inside = jsonResult.objectForKey("soon2challenger")

现在您已经提取了 soon2challenger 对象。然后就可以轻松获取值了:

print(include.objectForKey("id"))

关于ios - 无法返回正确的 JSON 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31145609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com