我如何像这样解析 JSON:
let json = "{\"key\":18446744073709551616}"
struct Foo: Decodable {
let key: UInt64
}
let coder = JSONDecoder()
let test = try! coder.decode(Foo.self, from: json.data(using: .utf8)!)
Run Code Online (Sandbox Code Playgroud)
问题是这个数字对于UInt64. 我知道 Swift 中没有更大的整数类型。
Parsed JSON number <18446744073709551616> does not fit in UInt64
Run Code Online (Sandbox Code Playgroud)
我不介意把它作为Stringor Data,但这是不允许的,因为JSONDecoder知道它应该是一个数字:
Expected to decode String but found a number instead.
Run Code Online (Sandbox Code Playgroud) 我有一个 JSON 文件,看起来像这样:
{
"items" : [
{ "name": "a name", "version": "a version" },
{ "version": "a 2nd version" },
{
"any_of": [
{ "name": "some name" },
{ "name": "some other name", "version": "some other version" },
[
{ "name": "another name" },
{ "version": "another version" },
{
"any_of": [
[
{ "version": "some version" },
{ "version": "some version" }
],
{ "version": "yet another version" }
]
}
]
]
},
{
"any_of" : [ …Run Code Online (Sandbox Code Playgroud) 我有一个符合可解码的结构。它有 50 个 String 属性和只有一个 Bool。该 bool 来自服务器,如字符串“false”/“true”或有时像整数 0/1,因此无法从框中解码。我怎样才能让它解码但不写大量的所有 50 个字符串属性的手动解码?也许以某种方式覆盖 Bool 的 decodeIfPresent,但我无法让它工作。我如何才能避免通过手动解码所有内容和所有内容并仅处理那个 Bool 来创建 init?如果可能,没有计算属性。
struct Response: Decodable {
var s1: String
var s2: String
var s3: String
//...........
var s50: String
var b1: Bool
var b2: Bool
}
Run Code Online (Sandbox Code Playgroud)
这是 json 示例:
{
"s1":"string"
"s2":"string"
"s3":"string"
//..........
"s50":"string"
"b1":"true"
"b2":"0"
}
Run Code Online (Sandbox Code Playgroud)
试过这个但不起作用(((
extension KeyedDecodingContainer { //Doesn't work, no execution
func decodeIfPresent(_ type: Bool.Type, forKey key: K) throws -> Bool {
return try! self.decodeIfPresent(Bool.self, forKey: key)
}
func decodeIfPresent(_ …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的 JSON
[
{
"name": "car",
"color": "red"
},
{
"name": "bike",
"color": "blue"
},
... etc
]
Run Code Online (Sandbox Code Playgroud)
我在解码过程中可以使用这个模型
struct Element:Codable {
var name:String?
var color:String?
}
Run Code Online (Sandbox Code Playgroud)
稍后在代码中我有ScrollView这样的
ScrollView(.vertical, showsIndicators: false) {
LazyVGrid(columns: gridItemLayout, alignment: .center, spacing: 10, content: {
ForEach(allElements) { element in
Run Code Online (Sandbox Code Playgroud)
allElements是一个数组Element。
因为我allElements在那里使用,所以我必须使其Element可识别......
struct Element:Codable, Identifiable {
var id = UUID()
var name:String?
var color:String?
}
Run Code Online (Sandbox Code Playgroud)
现在它无法解码 JSON,因为 JSON 没有id.
我尝试添加
"id" : "",
Run Code Online (Sandbox Code Playgroud)
JSON 上的所有元素均未成功。 …
我有以下 JSON:
\n[\n{\n "id": 1,\n "type": "Feature",\n "geometry": {\n "type": "Point",\n "coordinates": [\n 37.5741167,\n 55.7636592\n ]\n },\n "properties": {\n "hintContent": "\xd0\xbf\xd0\xb5\xd1\x80\xd0\xb5\xd1\x83\xd0\xbb\xd0\xbe\xd0\xba \xd0\x92\xd0\xbe\xd0\xbb\xd0\xba\xd0\xbe\xd0\xb2, 13\xd1\x811",\n "balloonContentHeader": "\xd0\xbf\xd0\xb5\xd1\x80\xd0\xb5\xd1\x83\xd0\xbb\xd0\xbe\xd0\xba \xd0\x92\xd0\xbe\xd0\xbb\xd0\xba\xd0\xbe\xd0\xb2, 13\xd1\x811"\n }\n]\nRun Code Online (Sandbox Code Playgroud)\n我正在尝试使用 JSONDecoder:
\nstruct Point : Codable {\n let id: Int\n let type: String\n let properties: Properties\n}\nstruct Properties : Codable {\n let hintContent: String\n let balloonContentHeader: String\n}\nstruct Points : Codable {\n var data : [Point]\n}\n\n\n\nfunc parse(fileName: String) {\n\n let url = Bundle.main.url(forResource: fileName, withExtension: "json")\n let data = try? Data(contentsOf: …Run Code Online (Sandbox Code Playgroud) 我想Any?用 Swift 保存(到 FileManager)。
它可以是一切,也可以是不符合 Codable 的类型(例如SCNNodes)。
我想将其包装到 Data 中。我知道,我不能用PropertyListEncoder.
您能让我轻松理解它是如何工作的吗?