【问题标题】:How to populate an array with a data from a JSON File [duplicate]如何使用 JSON 文件中的数据填充数组 [重复]
【发布时间】:2016-08-19 15:16:27
【问题描述】:

我正在开发一个读取购物中心名称和坐标的应用程序。然后它将这些与用户的当前位置进行比较,以便用户只能看到他周围最近的购物中心。出于这个原因,我正在考虑将所有名称和位置放入 2 个数组中,并将它们与用户的当前位置进行比较。然后我会为他们准备 segue 并将它们发送到我的TableViewController,在那里我将在不同的单元格中显示它们中的每个人。

我是 Swift 新手,如果有人能帮助我如何做到这一点,我会很高兴。

我的 JSON 文件是:

{
    "People": {
        "Person0": {
            "A1": "New York",
            "B1": "ShoppingMall1",
            "C1": "43.0757",
            "D1": "23.6172"
        },

        "Person1": {
            "A1": "London",
            "B1": "ShoppingMall2",
            "C1": "44.0757",
            "D1": "24.6172"
        },
        "Person2": {
            "A1": "Paris",
            "B1": "ShoppingMall3",
            "C1": "45.0757",
            "D1": "25.6172"
        },
        "Person3": {
            "A1": "Bern",
            "B1": "ShoppingMall4",
            "C1": "41.0757",
            "D1": "21.6172"
        },
        "Person4": {
            "A1": "Sofia",
            "B1": "ShoppingMall5",
            "C1": "46.0757",
            "D1": "26.6172"

        }
    }
}

【问题讨论】:

  • 建议:不要使用带有 PersonX 键的字典,而是在 JSON 中使用人员数组。

标签: arrays json swift


【解决方案1】:

没有第三方库:

斯威夫特 3:

func convertStringToDictionary(text: String) -> [String:AnyObject]? {
    if let data = text.data(using: String.Encoding.utf8) {
        do {
            return try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject]
        } catch let error as NSError {
            print(error)
        }
    }
    return nil
}

斯威夫特 2:

func convertStringToDictionary(text: String) -> [String:AnyObject]? {
    if let data = text.dataUsingEncoding(NSUTF8StringEncoding) {
        do {
            return try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject]
        } catch let error as NSError {
            print(error)
        }
    }
    return nil 
}

然后你有一本字典,你可以像这样访问你的人民:

let dict = convertStringToDictionary(jsonText)
print(dict["People"])

使用你的 json 你不能有一个数组,因为你应该像这样改变 json:

{
    "People": [{
            "A1": "New York",
            "B1": "ShoppingMall1",
            "C1": "43.0757",
            "D1": "23.6172"
        },
        {
            "A1": "London",
            "B1": "ShoppingMall2",
            "C1": "44.0757",
            "D1": "24.6172"
        }, {
            "A1": "Paris",
            "B1": "ShoppingMall3",
            "C1": "45.0757",
            "D1": "25.6172"
        }, {
            "A1": "Bern",
            "B1": "ShoppingMall4",
            "C1": "41.0757",
            "D1": "21.6172"
        }, {
            "A1": "Sofia",
            "B1": "ShoppingMall5",
            "C1": "46.0757",
            "D1": "26.6172"

        }
    ]
}

【讨论】:

  • 非常感谢您的回答。问题一:JSON文件的名字写在哪里,让app知道从哪个JSON文件获取数据
  • 这取决于你的 json 是在 app bundle 中还是从服务器下载
  • 它来自一个 URL 链接
  • @IvanSosev 阅读本指南:learnswiftonline.com/mini-tutorials/…
  • 谢谢,我可以做到,但我仍然无法将 savevd 的确切内容保存到字典中。我必须比较位置,如果我比较整个字典,我也会比较保存的名称。我不想比较它们我只需要 JSON 文件中的 C1 和 D1,名称 A1 和 B1 必须显示在另一本词典
猜你喜欢
  • 1970-01-01
  • 2017-11-17
  • 2018-03-07
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-12
  • 1970-01-01
相关资源
最近更新 更多