如何解决通过参数从JSON获取数据集
尝试使用我的参数从JSON查找唯一数据集。
我有JSON“ userDetails”:
{
"userDetails": [
{
"name": "Sabine","imageUrl" :"https://randomuser.me/api/portraits/men/82.jpg","content": [
{
"type": "video","url": "https://storage.googleapis.com/coverr-main/mp4/Travaho.mp4"
},{
"type": "image","url": "https://picsum.photos/640/1136/?image=281"
}
]
},{"name": "Keila Maney","imageUrl" :"https://randomuser.me/api/portraits/women/81.jpg","content": [
{
"type": "image","url": "https://picsum.photos/640/1136/?image=273"
},{
"type": "video","url": "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4"
}
]
}
]
}
我知道我的用户是 Keila Maney (我的代码中已经包含了users属性),并且需要将该参数发送到JSON,并且仅接收 Keila Maney strong>属性(它是imageUrl,内容(类型,URL)),然后解析它们。
是否有用于搜索JSON的标准方法?
谢谢!
解决方法
只需创建UserDetails的对象,例如:var userData: [UserDetails]?
并在循环userData[x].name == "Keila Maney"
中进行检查。那么这个x将成为您对Keila Maney的索引,您可以获取其所有数据。将您的json数据绑定到UserModel对象之后。这是您将从json创建的三个类
-
UserModel
struct UserModel : Codable { let userDetails : [UserDetails]? enum CodingKeys: String,CodingKey { case userDetails = "userDetails" } init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) userDetails = try values.decodeIfPresent([UserDetails].self,forKey:.userDetails) } }
-
UserDetails
struct UserDetails : Codable { let name : String? let imageUrl : String? let content : [Content]? enum CodingKeys: String,CodingKey { case name = "name" case imageUrl = "imageUrl" case content = "content" } init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) name = try values.decodeIfPresent(String.self,forKey: .name) imageUrl = try values.decodeIfPresent(String.self,forKey: .imageUrl) content = try values.decodeIfPresent([Content].self,forKey: .content) } }
-
内容
struct Content : Codable { let type : String? let url : String? enum CodingKeys: String,CodingKey { case type = "type" case url = "url" } init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) type = try values.decodeIfPresent(String.self,forKey: .type) url = try values.decodeIfPresent(String.self,forKey: .url) } }
Json示例:
{
"places": [
{
"name": "Ulrikh","id": 555,},{
"name": "Place 2","id": 1,}
]
}
致电:
self.getDataByID(id: 555)
快速方法:
func getDataByID(id : Int) {
let data = self.getData()
for i in 0..<data.count
{
if(Int((((data as NSDictionary)["places"] as! NSArray)[i] as! NSDictionary)["id"] as! Int) == id)
{
print("here is result:")
print((((data as NSDictionary)["places"] as! NSArray)[i]))
}
}
}
func getData() -> NSDictionary {
do{
let path = Bundle.main.path(forResource: "test",ofType: "json")
let jsonData = NSData(contentsOfFile: path!)
let jsonResult:NSDictionary! = try JSONSerialization.jsonObject(with: jsonData! as Data,options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
return jsonResult
} catch let error as NSError {
print(error)
}
return NSDictionary()
}
不,谢谢:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。