Pitaya icon indicating copy to clipboard operation
Pitaya copied to clipboard

JSONND null 值问题,不知如何 解决

Open miws opened this issue 7 years ago • 7 comments

let j = JSONND(string: "{\"x\":null}") print(j ) if let x = j["x"].RAW { print(x) } 这样无法正常执行下去,到if let 语句就崩溃了,不知如何解决

miws avatar Jun 03 '17 11:06 miws

null 不会崩,空字符串就更不会崩了,你的问题出在这里:

let j = JSONND(string: "{"x":null}")

这一行代码根本就无法编译通过,因为语法错误。确实,Swift 中只能使用双引号表示字符串,而恰好 JSON 格式也只能使用双引号来表示 key 以及字符串类型的 value,所以这行代码应该这么写:

let j = JSONND(string: "{\"x\":null}")

johnlui avatar Jun 04 '17 07:06 johnlui

我是加了转义符的,只是copy进来时不显示了.

在 if let 处报崩溃了.

miws avatar Jun 04 '17 08:06 miws

**

    let j = JSONND(string: "{\"x\":null}")
    print(j )

    if let x = j["x"].RAW {
        print(x)
    }

** 在if let 处崩溃,代码停留在 JSONND.swift 82行

miws avatar Jun 04 '17 08:06 miws

控制台输出

JSONND(data: { x = ""; }) 2017-06-04 17:00:12.035 xxx[99408:16529991] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write' *** First throw call stack: ( 0 CoreFoundation 0x000000010cf35b0b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010a81e141 objc_exception_throw + 48 2 CoreFoundation 0x000000010cf9e625 +[NSException raise:format:] + 197 3 Foundation 0x000000010a317e78 +[NSJSONSerialization dataWithJSONObject:options:error:] + 253 4 Pitaya 0x000000010a2a96e4 TFV6Pitaya6JSONNDg3RAWGSqSS + 196 5 xxx 0x000000010a201073 _TFC3xxx11AppDelegate11applicationfTCSo13UIApplication29didFinishLaunchingWithOptionsGSqGVs10DictionaryVSC29UIApplicationLaunchOptionsKeyP____Sb + 835 6 xxx 0x000000010a201354 _TToFC3xxx11AppDelegate11applicationfTCSo13UIApplication29didFinishLaunchingWithOptionsGSqGVs10DictionaryVSC29UIApplicationLaunchOptionsKeyP____Sb + 180 7 UIKit 0x000000010ace6957 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 299 8 UIKit 0x000000010ace823c -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4119 9 UIKit 0x000000010acee584 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1709 10 UIKit 0x000000010aceb793 -[UIApplication workspaceDidEndTransaction:] + 182 11 FrontBoardServices 0x000000011339e5f6 FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK + 24 12 FrontBoardServices 0x000000011339e46d -[FBSSerialQueue _performNext] + 186 13 FrontBoardServices 0x000000011339e7f6 -[FBSSerialQueue _performNextFromRunLoopSource] + 45 14 CoreFoundation 0x000000010cedbc01 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 15 CoreFoundation 0x000000010cec10cf __CFRunLoopDoSources0 + 527 16 CoreFoundation 0x000000010cec05ff __CFRunLoopRun + 911 17 CoreFoundation 0x000000010cec0016 CFRunLoopRunSpecific + 406 18 UIKit 0x000000010acea02f -[UIApplication _run] + 468 19 UIKit 0x000000010acf00d4 UIApplicationMain + 159 20 xxx 0x000000010a201be7 main + 55 21 libdyld.dylib 0x000000010e51665d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

miws avatar Jun 04 '17 09:06 miws

我搜了一下 Invalid top-level type in JSON write 这个错误,发现是在生成 JSON 字符串的时候抛出的,就是 .RAW 的时候。

johnlui avatar Jun 04 '17 09:06 johnlui

用这个方法试试吧,直接将子对象使用 JSONND 对象初始化:

class TestModel: JSONNDModel {
    var hey: Hey!
    required init(JSONNDObject json: JSONND) {
        super.init(JSONNDObject: json)

        self.hey = Hey(JSONNDObject: json["hey"])
    }
}
class Hey: JSONNDModel {
    var oo = "xx"
}

johnlui avatar Jun 04 '17 09:06 johnlui

`

    super.init(JSONNDObject: json)
    self.hey = Hey(JSONNDObject: json["hey"])

` 这两句都会成功执行,所以并不能获得一个nil的对象,仍然无法解决问题

miws avatar Jun 06 '17 03:06 miws