如何快速解析数据库中的JSON数组

时间:2018-06-29 07:50:16

标签: json swift

在我的应用中,我正在尝试解析JSON文件。

这是我的JSON:

  

[{“ locals”:[{“ name”:“ Mission Chinese Food”,“ localDescription”:“ A”,“ photograph”:“ img_02.jpg”,“ address”:“ 171 E Broadway,纽约,NY 10002”,“时间表”:“ Lunedi al Sabato 08:00-12:00 e 14:00- 18:00 Domenica 08:00-12:00 e 14:00- 18:00”,“ numberOfReviews”: 10,“ numberInCity”:2,“ cityName”:“纽约”,“ ratingValue”:5},{“ locals”:[{“ name”:“ Mission Chinese Food”,“ localDescription”:“ A”,“照片”:“ img_02.jpg”,“地址”:“ 171 E Broadway,纽约,NY 10002”,“时间表”:“ Lunedi al Sabato 08:00-12:00 e 14:00- 18:00多梅尼卡08 :00-12:00 e 14:00- 18:00“,” numberOfReviews“:10,” numberInCity“:2,” cityName“:”纽约“,” ratingValue“:5},]

这是我的快捷文件:

let url = URL(string: "https://www.litos.srl/showtime/dbGetLocal.php")

do {
    let data = try Data(contentsOf: url!)
    print(data[0])

    guard let rootObject = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[String:AnyObject]]  else {
    return locals
}

print(rootObject)
let localsObjects = rootObject[0]["locals"] as? [[String:AnyObject]]

    } catch {
        print(error)
        return locals
    }
    return locals
}

这是我构建和运行代码时在Xcode中发生的错误:

  

60   错误域= NSCocoaErrorDomain代码= 3840“字符0周围的值无效。” UserInfo = {NSDebugDescription =字符0周围的值无效。}   2018-06-29 09:44:19.293880 + 0200 eventsProject [13504:4365358] [BoringSSL]函数boringssl_session_errorlog:第2871行[boringssl_session_read] SSL_ERROR_ZERO_RETURN(6):操作失败,因为使用close_notify警报完全关闭了连接   2018-06-29 09:44:19.294067 + 0200 eventsProject [13504:4365358] [BoringSSL]函数boringssl_session_errorlog:第2871行[boringssl_session_read] SSL_ERROR_ZERO_RETURN(6):操作失败,因为连接已通过close_notify警报完全关闭   2018-06-29 09:44:19.294258 + 0200 eventsProject [13504:4365358] [BoringSSL]函数boringssl_session_errorlog:第2871行[boringssl_session_read] SSL_ERROR_ZERO_RETURN(6):操作失败,因为使用close_notify警报完全关闭了连接

如何解析JSON并快速获取数据?

3 个答案:

答案 0 :(得分:0)

enter image description here您的数据无效。该数据不应以] 之前的结尾。

答案 1 :(得分:0)

尝试这个。如果您的JSON有效,这将起作用。

例如。

[{“ locals”:[     {“名称”:“中式食品”,“ localDescription”:“ A”,“照片”:“ img_02.jpg”,“地址”:“ 171 E Broadway,纽约,纽约10002”,“时间表”:“ Lunedi al Sabato 08:00-12:00 e 14:00- 18:00 Domenica 08:00-12:00 e 14:00- 18:00“,” numberOfReviews“:” 10“,” numberInCity“:” 2 “,” cityName“:”纽约“,” ratingValue“:” 5“}]}]]

if let data = JSON as? NSArray {
        for data in data {
            if let data = data as? [String: AnyObject] {
                if let data = data["locals"] as? NSArray {
                    for data in data {
                        if let data = data as? [String: AnyObject] {

                        }
                    }
                }
            }
        }
    }

答案 2 :(得分:0)

您的网址响应数据缺少功能,请检查,字典没有正确的结尾。

以下是您的Json回复

  

{“ id”:0,“名称”:“中式食品”,“ localDescription”:“ A”,“照片”:“ img_02.jpg”,“地址”:“ 171 E Broadway,纽约, NY 10002”,“时间表”:“ Lunedi al Sabato 08:00-12:00 e 14:00- 18:00 Domenica 08:00-12:00 e 14:00- 18:00”,“ numberOfReviews”:10 ,“ numberInCity”:2,“ cityName”:“纽约”,“ ratingValue”:5}   stdClass对象([locals] => 0){“ id”:1,“ name”:“ Mission Chinese Food”,“ localDescription”:“ B”,“ photograph”:“ img_02.jpg”,“ address”:“ 171 E Broadway,New York,NY 10002“,“时间表”:“ Lunedi al Sabato 08:00-12:00 e 14:00- 18:00 Domenica 08:00-12:00 e 14:00- 18:00 “,” numberOfReviews“:10,” numberInCity“:2,” cityName“:”纽约“,” ratingValue“:5}   stdClass对象([locals] => 0){“ id”:2,“ name”:“哈特人朱巴”,“ localDescription”:“ V”,“ photograph”:“ img_02.jpg”,“ address”:“ 171 E Broadway,New York,NY 10002“,“时间表”:“ Lunedi al Sabato 08:00-12:00 e 14:00- 18:00 Domenica 08:00-12:00 e 14:00- 18:00 “,” numberOfReviews“:10,” numberInCity“:2,” cityName“:”纽约“,” ratingValue“:5}   stdClass对象([locals] => 0)   stdClass对象([locals] => 0)