Ios 通过参数从JSON获取数据集

Ios 通过参数从JSON获取数据集,ios,json,swift,parsing,search,Ios,Json,Swift,Parsing,Search,尝试使用my参数从JSON中查找唯一的数据集 我有JSON“userDetails”: } 我知道我的用户是Keila Maney(我的代码中有users属性),需要将该参数发送到JSON,然后只接收Keila Maney属性(它是imageUrl,content(type,url)),然后解析它们 在JSON中有没有标准的搜索方法 提前谢谢 Json示例: { "places": [ { "name": "U

尝试使用my参数从JSON中查找唯一的数据集

我有JSON“userDetails”:

}

我知道我的用户是Keila Maney(我的代码中有users属性),需要将该参数发送到JSON,然后只接收Keila Maney属性(它是imageUrl,content(type,url)),然后解析它们

在JSON中有没有标准的搜索方法

提前谢谢

Json示例:

{  
  "places": [  
    {  
      "name": "Ulrikh",
      "id": 555,
    },  
    {  
      "name": "Place 2",  
      "id": 1,  
    }
]  
} 
电话:

Swift方法:

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()
    }
func getDataByID(id:Int){
让data=self.getData()
对于0..NSDictionary中的i{
做{
让path=Bundle.main.path(forResource:“test”,类型为:“json”)
让jsonData=NSData(contentsOfFile:path!)
让jsonResult:NSDictionary!=尝试将JSONSerialization.jsonObject(使用:jsonData!作为数据,选项:JSONSerialization.ReadingOptions.mutableContainers)作为!NSDictionary
返回jsonResult
}将let错误捕获为NSError{
打印(错误)
}
返回NSDictionary()
}

不感谢:)

只需创建UserDetails对象,如:
var userData:[UserDetails]?
并在循环中检查此项
userData[x]。name==“Keila Maney”
。然后此x将是Keila Maney的索引,您可以获取其所有数据。将json数据绑定到UserModel对象后。这是您将从json创建的三个类

  • 用户模型

    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)
    }
    
    }
    
  • 用户详细信息

    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
    ?没有标准。这取决于后端。这意味着在JSON中,我有很多记录,我需要用我的参数过滤掉它们(在我的例子中,参数是“name”=“Keila Maney”)
    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)
    }
    
    }