最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中如何初始化用JSON字符串初始化SwiftyJSON变量

JSON crifan 3020浏览 0评论

已有字符串:

sceneStrNoSlash={"0":{"user-12059519-b8e0-4860-bd20-4e74840fb70a":0,"user-2a00aaac-074c-4985-84e9-7ee724742d53":0,"user-26d642fc-6d1f-40ef-9220-658b98ccb6bd":0,"user-757f84e3-7b60-4264-a21f-2ea48e3b922c":0,"user-b28a76c7-0c58-4092-abf6-42f1878484d3":0,"user-ef5e0d35-7858-4777-9299-3c0e69a933c6":0,"user-736899be-1d71-4f0a-9186-04ea3a136198":0},"1":{"group-25edbda2-9e6b-49f6-a45a-73f2052565bb":0,"group-694475a6-a718-43ce-a5bc-95fbba00f524":0},"2":{"group-5d68f640-f6ef-4c1a-a705-bb6d1fa8435e":0,"group-67f90e86-8b4c-4cc0-8872-802a80c64c2d":0,"group-0ff80a07-f7f1-484a-9784-795b62d5a049":0},"3":{"user-fe78066b-5085-4d9b-a731-1746efa80968":0,"user-62d41ad4-11d4-4c0f-b1b1-5019b7c77663":0,"user-00000000-0000-0000-0000-000000000002":0,"user-736899be-1d71-4f0a-9186-04ea3a136198":0,"user-757f84e3-7b60-4264-a21f-2ea48e3b922c":0,"user-79aa0f8b-c346-4668-bddd-9b5c9e936117":0,"user-3ec11e86-29ad-4576-be9b-8228e186e8a1":0,"user-b18b54f8-287b-4794-a4ed-15adfce8d578":0,"user-9c84c90c-e1bd-45a8-b401-c97d36c8b7bc":0,"user-12059519-b8e0-4860-bd20-4e74840fb70a":0,"user-2a00aaac-074c-4985-84e9-7ee724742d53":0,"user-cbcaae2f-ecec-44b0-84fb-c99dd4c4726a":0,"user-26d642fc-6d1f-40ef-9220-658b98ccb6bd":0,"user-64d8401d-94b5-4ff3-92a4-581e153a1fc9":0,"user-b28a76c7-0c58-4092-abf6-42f1878484d3":0,"group-0463c067-ecdf-4d91-baf6-3583679379cb":0,"group-2a7ea22d-a74a-4fde-ac31-691b301cd89c":0,"group-bc6c9b32-d100-4555-8d55-8c40f6a6ae54":0,"group-2484009d-8fa1-4a0b-922f-88ef0f826ec0":0,"group-647da8e4-ecf5-4720-9517-89dbc0b0cffe":0}}

想要转换为字符串为SwiftyJSON变量。

试了:

                let sceneJson = JSON.init(stringLiteral: StringLiteralType.init(stringLiteral: sceneStrNoSlash))
                print("sceneJson=\(sceneJson)")

不行。

swift init SwiftyJSON with json string

json – Initialize Swift class with AnyObject? from NSJSONSerialization – Stack Overflow

看了:

// MARK: – JSON Base
public struct JSON {
    /**
    Creates a JSON using the data.
    – parameter data:  The NSData used to convert to json.Top level object in data is an NSArray or NSDictionary
    – parameter opt:   The JSON serialization reading options. `.AllowFragments` by default.
    – parameter error: error The NSErrorPointer used to return the error. `nil` by default.
    – returns: The created JSON
    */
    public init(data:NSData, options opt: NSJSONReadingOptions = .AllowFragments, error: NSErrorPointer = nil) {
        do {
            let object: AnyObject = try NSJSONSerialization.JSONObjectWithData(data, options: opt)
            self.init(object)
        } catch let aError as NSError {
            if error != nil {
                error.memory = aError
            }
            self.init(NSNull())
        }
    }
    /**
    Creates a JSON using the object.
    – parameter object:  The object must have the following properties: All objects are NSString/String, NSNumber/Int/Float/Double/Bool, NSArray/Array, NSDictionary/Dictionary, or NSNull; All dictionary keys are NSStrings/String; NSNumbers are not NaN or infinity.
    – returns: The created JSON
    */
    public init(_ object: AnyObject) {
        self.object = object
    }

去试试:

                let sceneJson = JSON(sceneStrNoSlash)
                print("sceneJson=\(sceneJson)")

好像没用?

swift init SwiftyJSON  string

ios – SwiftyJSON keeps returning empty objects – Stack Overflow

后来用:

let sceneData:NSData = sceneStrNoSlash.dataUsingEncoding(NSUTF8StringEncoding)!
//print("sceneData=\(sceneData)")
let sceneJson = JSON(data: sceneData)

就可以了。

[总结]

对于已经是Json格式的字符串的话,通过:

let jsonData:NSData = inputJsonStr.dataUsingEncoding(NSUTF8StringEncoding)!
let jsonValue = JSON(data: jsonData)

即可得到SwiftyJSON类型的变量了。

转载请注明:在路上 » [已解决]swift中如何初始化用JSON字符串初始化SwiftyJSON变量

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.195 seconds, using 22.06MB memory