与以下json对应的数据模型是什么?
df=pd.DataFrame({'col1':['x','y','z'],'col3':[[('ab','cv'), ('da','ndfds')],[('ab','cv')],[('dsfsa','sfa'), ('sd','sfag')]]})
l=[('ab','cv'),('da','ndfds')]
这是字典的字典 所以我尝试了,
{
dog:
{
type: "dog",
logoLocation: "url1"
},
pitbull:
{
type: "pitbull",
logoLocation: "url2"
}
}
但是它不起作用。有什么帮助吗?
答案 0 :(得分:3)
您需要
let calcError2 filter (fcalc:'a -> float) (arr:'a array) =
let subarr = arr |> Array.filter filter
if Array.isEmpty subarr then Result.Error "array length 0"
else subarr |> Array.averageBy fcalc |> Result.Ok
更正json
struct Root: Codable {
let dog, pitbull: Dog
}
struct Dog: Codable {
let type, logoLocation: String // or let logoLocation:URL
}
动态
只需在解码器中使用{
"dog":
{
"type": "dog",
"logoLocation": "url1"
},
"pitbull":
{
"type": "pitbull",
"logoLocation": "url2"
}
}
[String:Dog]
答案 1 :(得分:0)
我会跳过第一堂课,然后继续
class PhotoModel: Codable {
var type: String
var logoLocation: String
}
然后像字典一样对其进行解码
do {
let decoder = JSONDecoder()
let result = try decoder.decode([String: PhotoModel].self, from: data)
result.forEach { (key,value) in
print("Type: \(value.type), logo: \(value.logoLocation) (key: \(key))")
}
} catch {
print(error)
}
输出
类型:狗,徽标:url1(键:狗)
类型:pitbull,徽标:url2(键:pitbull)
两个属性确实都是可选的,如果不是,我建议您删除?
中任何不必要的PhotoModel
(我确实如此)