Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
转换要提取的JSON对象时出错 ;来自it的数据_Json_Swift_Types_Casting - Fatal编程技术网

转换要提取的JSON对象时出错 ;来自it的数据

转换要提取的JSON对象时出错 ;来自it的数据,json,swift,types,casting,Json,Swift,Types,Casting,我有一些JSON看起来像这样 [ { "schema_name": "major_call", "schema_title": "Major Call", "schema_details": [ { "dataname": "call_number", "title": "Call Number", "datatype": "viewtext" }, 还有一些代码

我有一些JSON看起来像这样

[
  {
    "schema_name": "major_call",
    "schema_title": "Major Call",
    "schema_details": [
        {
            "dataname": "call_number",
            "title": "Call Number",
            "datatype": "viewtext"
        },
还有一些代码来处理它

let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [[String:Any]]
for i in 0 ..< json.count{
     let schema_name: String = json[i]["schema_name"] as? String ?? "" //works fine!
     print(schema_name)
     // error: Contextual type '[String : Any]' cannot be used with array literal
     let blob: [String:Any] = json[i]["schema_details"] as? [String:Any] ?? [""] 

     for j in 0 ..< blob.count{ //this is all I want to do!
         // errror: Cannot subscript a value of type '[String : Any]' with an index of type 'Int'
         let data_name: String = blob[j]["dataname"] as? String ?? "" 
         print(schema_name +  "." + data_name)

     }
}
let json=尝试JSONSerialization.jsonObject(使用:data,options:.allowFragments)作为![[String:Any]]
对于0中的i..
但它不会解析嵌套对象。我在标记的行上得到一个错误,即对象的类型不正确


解包数据需要使用哪些类型?

schema\u details
的值是数组,而不是字典

为了清楚起见,让我们使用一个类型别名并删除丑陋的基于C样式索引的循环

typealias JSONArray = [[String:Any]]

if let json = try JSONSerialization.jsonObject(with: data) as? JSONArray {
    for schema in json {
        let schemaName = schema["schema_name"] as? String ?? ""
        print(schemaName)
        if let details = schema["schema_details"] as? JSONArray {  
            for detail in details { 
                let dataName = detail["dataname"] as? String ?? "" 
                print(schemaName +  "." + dataName)
            }
        }
    }
}

key
schema_details
的值是数组,而不是字典

为了清楚起见,让我们使用一个类型别名并删除丑陋的基于C样式索引的循环

typealias JSONArray = [[String:Any]]

if let json = try JSONSerialization.jsonObject(with: data) as? JSONArray {
    for schema in json {
        let schemaName = schema["schema_name"] as? String ?? ""
        print(schemaName)
        if let details = schema["schema_details"] as? JSONArray {  
            for detail in details { 
                let dataName = detail["dataname"] as? String ?? "" 
                print(schemaName +  "." + dataName)
            }
        }
    }
}

提示:
json[i][“schema_details”]
返回一个字典数组,就像json的顶层一样。就这些吗
let blob=json[i][“schema_details”]
MAN如果您使用的是Swift 4,我强烈建议您使用新的
Codable
API和
JSONDecoder
。提示:
json[i][“schema_details”
返回一个字典数组,就像json的顶级一样。就这些吗
let blob=json[i][“schema_details”]
MAN您是个天才如果您使用的是Swift 4,我强烈建议您使用新的
Codable
API和
JSONDecoder
vadian
,这太棒了,我现在有点小麻烦了,我需要稍后对该词典的一部分进行字符串化以进行缓存,如何将
detail
转换为
字符串(数据:,编码:.utf8)
编码所需的
Data:
detail
是一个字典。您可以使用
JSONSerialization
来做相反的事情,将字典序列化为
数据
表示JSON字符串。
vadian
太好了,我现在有一点问题,我需要稍后将字典的一部分字符串化以进行缓存,如何将
detail
转换为
字符串(数据:,编码:.utf8)
编码所需的
Data:
detail
是一个字典。您可以使用
JSONSerialization
执行相反的操作,并将字典序列化为表示JSON字符串的
Data