【发布时间】:2020-10-18 05:33:30
【问题描述】:
问题一直存在
我正在尝试能够将我的 json 文件数据读出到控制台日志中进行测试,以便我以后可以使用它。
由于不同的数据大小和可能的错误 json 格式,我不确定如何完成我的其他结构。
一旦完成,我相信我需要使用 for 循环从“M”、“S”和“WP”读取不同大小的数据(我相信这部分不应该很复杂)
可能需要考虑的事项
我想向“M”“S”“WP”写入和添加数据
("M", "S") 的数据量可以是任意数量的字符串数组数据对象
“WP”中的数据可能需要不同的格式我想添加一个名称(“abc”)和一个包含任意数量数据点的 Int 数组
注意:我的 Json 格式在 MP 和 WP 的某些方面可能有误
获取数据的 Swift 代码
import Foundation
struct UserDay: Codable {
let mp: UserMP
let wp: UserWP
}
struct UserMP: Codable {
let m: [UserM]
let s: [UserS]
}
struct UserM : Codable {
let title: String
let description: String
let time: String
}
struct UserS : Codable {
let title: String
let description: String
let time: String
}
struct UserWP: Codable {
let wp: [WPData]
}
struct WPData: Codable {
let title: String
let values: [Int]
}
class LogDataHandler {
public func grabJSONInfo(){
guard let jsonURL = Bundle(for: type(of: self)).path(forResource: "LogData", ofType: "json") else { return }
guard let jsonString = try? String(contentsOf: URL(fileURLWithPath: jsonURL), encoding: String.Encoding.utf8) else { return }
// Print Info for TESTING
var year: UserDay?
do {
year = try JSONDecoder().decode(UserDay.self, from: Data(jsonString.utf8))
} catch {
print("ERROR WHEN DECODING JSON")
}
guard let results = year else {
print("YEAR IS NIL")
return
}
print(results)
}
}
下面的 JSON 示例数据
{
"01/01/2020": {
"MP" : {
"M" : [
{"title" : "m1", "description" : "1", "time" : "12:30pm"},
{"title" : "m2", "description" : "2", "time" : "1:30pm"},
{"title" : "m3", "description" : "3", "time" : "2:30pm"}
],
"S" : [
{"title" : "s1", "description" : "1", "time" : "1pm"}
]
},
"WP" : [
{ "title" : "abc", "values" : [12, 10, 6]},
{ "title" : "def", "values" : [8]}
]
},
"01/29/2020": {
"MP" : {
"M" : [{"title" : "m1", "description" : "1", "time" : "12:30pm"}],
"S" : [{"title" : "s1", "description" : "1", "time" : "12:30pm"}]
},
"WP" :[{ "title" : "def", "values" : [8]}]
}
}
【问题讨论】:
-
您可以将有时缺少的属性设为可选。例如,在
UserWP中,您将拥有:var abc, def: [Int]?。但是,M和S不是带有{ [...],[...] }的有效 JSON,因此如果您想使用JSONDecoder对其进行解码,则需要修复它 -
@NewDev 我更新了我的 json 和代码,但我的输出是 ERROR WHEN WHEN DECODING JSON and YEAR IS NIL
-
当我打印本地化错误
The data couldn’t be read because it isn’t in the correct format.时也会出现这个错误 -
您的 JSON 格式仍然不正确 - 使用任何在线 JSON 验证器站点来修复它(例如:this)。您将面临的另一个挑战是
monthX属性。这将需要手动解码。还有,"1"s 是什么意思?这总是"1"吗? -
@NewDev thats 1 只是一个天数,它只是显示第 1 天而不是第 1-30 天
标签: json swift struct encoding decoding