本文介绍了如何快速解析这个json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个请求

Alamofire.request(.GET,HttpHelper.baseURL+HttpHelper.tripsURL,encoding:.JSON).responseJSON {
response in 

    var json  = JSON(data: response.data!)
    print(json)
    print(json["res"])
}

按照结果

{
  "res" : "[{"name":"testName","lastName":"testLastName"},{"name":"testName","lastName":"testLastName"}]",
  "status" : "success",
  "out" : "{"name":"testName","lastName":"testLastName"}"
}
[{"name":"testName","lastName":"testLastName"},{"name":"testName","lastName":"testLastName"}]

如何将数据从 res 设置为 struct 或 class User

how i can set data from res to struct or class User

struct  User  {
    var name : String?
    var lastName : String?
}

请帮忙解决这个问题)非常感谢!!)

please help to solve this problem) thank you very much !!)

推荐答案

你可以这样做

var result: [User]()
for user in json["res"] {
   let userTmp = User(name: user["name"], lastName: user["lastName"])
   result.append(userTmp)
}

问候

这篇关于如何快速解析这个json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 14:47