iOS-无法将'String'类型的值转换为预期的参数类型'Data'

时间:2018-04-26 11:58:02

标签: ios json swift4

我有一个JSON数据作为字符串,我想将这些数据解码为对象列表,但我得到这个错误:“无法将'String'类型的值转换为预期的参数类型'数据'”

这是我的代码:

struct DatabaseObject: Decodable{
    var CityNameL = ""
    var DBName = ""
    var DBNameID = 0
    var IDName = ""
}

func jsonToObject(json:String){


    do{
     let databases = try  JSONDecoder().decode(DatabaseObject.self,from: json)
    }catch let jsonErr {
     print(jsonErr)
    }
 }

这是json数据:

[{"CityNameL":"Skeleton East","DBName":"SkeletonEAST","DBNameID":29,"IDName":"SKELETONEASTLOCAL"},{"CityNameL":"SKELETON DEMO/SKELETON DEMO","DBName":"SKELETONDEMO","DBNameID":30,"IDName":"SKELETONEASTDOT2"},{"CityNameL":"SKELETONPARAMS","DBName":"FFMSCENTRAL","DBNameID":31,"IDName":"FFMSCENTRALLOCAL"},{"CityNameL":"SKELECBM","DBName":"SKELETONFFMS","DBNameID":32,"IDName":"LOCAL"}]

有没有办法将String转换为数据,或者还有其他解码方法。

1 个答案:

答案 0 :(得分:0)

尝试将ID IdEsame IdParametro1 IdParametro2 IdParametro3 72 39 163 164 165 75 39 166 167 168 78 39 169 170 171 json转换为String

Data