gpt4 book ai didi

arrays - 如何在 Swift 中访问括号中的嵌套 JSON?

转载 作者:行者123 更新时间:2023-11-28 12:43:38 26 4
gpt4 key购买 nike

我有一个括号中的 JSON 响应,我很难访问内部字段,例如使用 Swift 显示名称。我该怎么做?

Optional(["result": {
user = {
"display_name" = "Max Test";
email = "test.max@gmail.com";
"fb_id" = 10209982554704497;
roles = (
stu
);
schools = "<null>";
};
}])

我用来访问 JSON 的代码:

 self.restApi.getProfileDetails() {responseObject, error in

//parse down the first layer of array
let response = responseObject as? [String:AnyObject]
print("response object when MyDetailsController opened")
print(response)

let result = response!["result"] as? [AnyObject]
print("result object")
print(result)

//parse down the second layer of JSON object
if let result = response!["result"] as? [AnyObject] {

print("result object when MyDetailsController opened")
// work with the content of "result", for example:
if let user = result[0] as? [String:AnyObject]{
print(user)

let displayName = user["display_name"]
print("displayName")
print(displayName)

}
}

似乎我以错误的方式处理结果,因为它始终为零:

控制台输出:

result object
nil

最佳答案

响应结果是dictionary不是array,尝试这样获取

let result = response!["result"]  as? [String:AnyObject]

关于arrays - 如何在 Swift 中访问括号中的嵌套 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38743156/

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