{
"StatusResponse":
{
"StatusCode": "000"
"StatusDescription": "Operation Success(000)"
"DebugDescription": "OperationSuccess"
}-
"memId": "3e369fec-a9c5-418b-a950-0647f7e15d7c"
"token": null
"isAdmin": false
"isTeacher": false
"isParent": true
"kinderId": null
}
这是我的JSON格式
Alamofire.request(.GET, "myURL").responseJSON { response in
switch response.result {
case .Success(let data):
let json = JSON(data)
let memId = json["memId"].stringValue
for result in json["StatusResponse"].arrayValue
{
let code = result["StatusCode"].stringValue
print("code = \(code)")
}
print("memId : \(memId)")
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
我的代码中有一个StatusResponse参数,但是,我可以获得memId字符串值,我的代码有什么问题我为什么不能在这里获取StatusCode? p>
答案 0 :(得分:1)
StatusResponse是字典而不是arrayValue,试试这个:
let code = json["StatusResponse"]["StatusCode"].stringValue
答案 1 :(得分:0)
看起来StatusResponse是该JSON示例中的字典,而不是数组。
编辑:我的斯威夫特现在有点生疏,但这应该是大致的样子:Alamofire.request(.GET, "myURL").responseJSON { response in
switch response.result {
case .Success(let data):
let json = JSON(data)
let memId = json["memId"].stringValue
if let statusResponse = json["StatusResponse"] as? NSDictionary
{
let code = statusResponse["StatusCode"].stringValue
print("code = \(code)")
}
print("memId : \(memId)")
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
答案 2 :(得分:0)
我对JSON格式有点混淆。什么是" - "在第三个括号之后?
如果JSON是这样的:
public partial class Entities : DbContext{
public Entities(): base("name=Entities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<GENERIC_FTP_SEND> GENERIC_FTP_SEND { get; set; }
}
然后memId不是StatusResponse对象的端口,因此你应该先通过展开来获取值(因为它可以为null)。
{
"StatusResponse":
{
"StatusCode": "000"
"StatusDescription": "Operation Success(000)"
"DebugDescription": "OperationSuccess"
},
"memId": "3e369fec-a9c5-418b-a950-0647f7e15d7c",
"token": null,
"isAdmin": false,
"isTeacher": false,
"isParent": true,
"kinderId": null
}
希望这有帮助!