【问题标题】:Find value json array swift快速查找值json数组
【发布时间】:2019-10-18 10:56:50
【问题描述】:

我正在尝试在我的 JSON 中查找特定值。

这是我的 json

[
  {
    "airportName" : "Simon Mwansa Kapwepwe Intl",
    "longitude" : 28.664999999999999,
    "geometry" : {
      "type" : "Point",
      "coordinates" : [
        28.664999999999999,
        -12.994999999999999
      ]
    },
    "countryCode" : "ZMB",
    "countryName" : "Zambia",
    "latitude" : -12.994999999999999,
    "cityName" : "Ndola",
    "airportCode" : "FLSK"
  },
  {
    "airportName" : "Mafikeng",
    "longitude" : 25.544469444444445,
    "geometry" : {
      "type" : "Point",
      "coordinates" : [
        25.544469444444445,
        -25.807447222222223
      ]
    },
    "countryCode" : "ZAF",
    "countryName" : "South African Rep",
    "latitude" : -25.807447222222223,
    "cityName" : "Mafikeng",
    "airportCode" : "FAMM"
  }]

现在,如果我编写此代码,它就可以工作了!

for item in 0...json.count {
    i = i+1
    if json[i]["airportName"] == "Simon Mwansa Kapwepwe Intl" {
        print ("I found it")
    }

如果我尝试将参数传递给一个不起作用的函数进行搜索,请迅速给我一个错误说:二进制运算符'=='不能应用于'JSON'和'String'类型的操作数

func cerca (nomeApt: String){
  var i = 0
  for item in 0...json.count {
    i = i+1
    if json[i]["airportName"] == nomeApt { // error I don't know
      print ("I found it")
    }
  }
}

老实说,我不知道为什么?知道如何解决这个问题吗?非常感谢

【问题讨论】:

  • 如果您使用Codable IMO,这会容易得多。题外话,但你的 for 循环很糟糕,它错过了数组中的第一个元素,最后会生成索引越界错误。

标签: arrays json swift


【解决方案1】:

你可以尝试使用可解码, 这将为您提供一个简单的数组,您可以在其中循环和访问属性。

import Foundation

let json = """
[
{
  "airportName" : "Simon Mwansa Kapwepwe Intl",
  "longitude" : 28.664999999999999,
  "geometry" : {
    "type" : "Point",
    "coordinates" : [
      28.664999999999999,
      -12.994999999999999
    ]
  },
  "countryCode" : "ZMB",
  "countryName" : "Zambia",
  "latitude" : -12.994999999999999,
  "cityName" : "Ndola",
  "airportCode" : "FLSK"
},
{
  "airportName" : "Mafikeng",
  "longitude" : 25.544469444444445,
  "geometry" : {
    "type" : "Point",
    "coordinates" : [
      25.544469444444445,
      -25.807447222222223
    ]
  },
  "countryCode" : "ZAF",
  "countryName" : "South African Rep",
  "latitude" : -25.807447222222223,
  "cityName" : "Mafikeng",
  "airportCode" : "FAMM"
}

]
""".data(using: .utf8)!


struct Geometry: Decodable {
    var type: String
    var coordinates: [Double]
}


struct Airports: Decodable {
    var airportName : String
    var longitude : Double
    var geometry: Geometry
    var countryCode : String
    var countryName: String
    var latitude: Double
    var cityName: String
    var airportCode :String
}



func findSpecifAirport(airportName: String) -> Bool {
    if let myStruct = try? JSONDecoder().decode([JsonDecoder].self, from: json) {
     let foundSpecific = myStruct.filter {  $0.airportName == airportName }
     return !foundSpecific.isEmpty;
    }
    return false
}

你可以这样使用它

let hasFoundIt = findSpecifAirport(airportName: "Simon Mwansa Kapwepwe Intl")

【讨论】:

  • 不要使用try!,因为问题是关于在数组中查找特定元素,您也应该包含相应的代码。
  • 你是对的@JoakimDanielson,我试图为他指出正确的方向而不是编写所有内容,我已经更新了我的答案以防万一......
【解决方案2】:

将json转成字符串,换行为:

if json[i]["airportName"] as? String == nomeApt {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 2012-03-22
    相关资源
    最近更新 更多