java 字符串转字典_如何将JSON字符串转换为字典?

使用Swift 3, JSONSerialization 有一个名为jsonObject(with:options:)的方法 . jsonObject(with:options:) 有以下声明:

class func jsonObject(with data: Data, options opt: JSONSerialization.ReadingOptions = []) throws -> Any

从给定的JSON数据返回Foundation对象 .

当您使用 jsonObject(with:options:) 时,您必须处理错误处理( try , try? 或 try! )并键入强制转换(来自 Any ) . 因此,您可以使用以下模式之一解决问题 .

#1 . 使用抛出并返回非可选类型的方法

import Foundation

func convertToDictionary(from text: String) throws -> [String: String] {

guard let data = text.data(using: .utf8) else { return [:] }

let anyResult: Any = try JSONSerialization.jsonObject(with: data, options: [])

return anyResult as? [String: String] ?? [:]

}

用法:

let string1 = "{\"City\":\"Paris\"}"

do {

let dictionary = try convertToDictionary(from: string1)

print(dictionary) // prints: ["City": "Paris"]

} catch {

print(error)

}

let string2 = "{\"Quantity\":100}"

do {

let dictionary = try convertToDictionary(from: string2)

print(dictionary) // prints [:]

} catch {

print(error)

}

let string3 = "{\"Object\"}"

do {

let dictionary = try convertToDictionary(from: string3)

print(dictionary)

} catch {

print(error) // prints: Error Domain=NSCocoaErrorDomain Code=3840 "No value for key in object around character 9." UserInfo={NSDebugDescription=No value for key in object around character 9.}

}

#2 . 使用抛出并返回可选类型的方法

import Foundation

func convertToDictionary(from text: String) throws -> [String: String]? {

guard let data = text.data(using: .utf8) else { return [:] }

let anyResult: Any = try JSONSerialization.jsonObject(with: data, options: [])

return anyResult as? [String: String]

}

用法:

let string1 = "{\"City\":\"Paris\"}"

do {

let dictionary = try convertToDictionary(from: string1)

print(String(describing: dictionary)) // prints: Optional(["City": "Paris"])

} catch {

print(error)

}

let string2 = "{\"Quantity\":100}"

do {

let dictionary = try convertToDictionary(from: string2)

print(String(describing: dictionary)) // prints nil

} catch {

print(error)

}

let string3 = "{\"Object\"}"

do {

let dictionary = try convertToDictionary(from: string3)

print(String(describing: dictionary))

} catch {

print(error) // prints: Error Domain=NSCocoaErrorDomain Code=3840 "No value for key in object around character 9." UserInfo={NSDebugDescription=No value for key in object around character 9.}

}

#3 . 使用不抛出并返回非可选类型的方法

import Foundation

func convertToDictionary(from text: String) -> [String: String] {

guard let data = text.data(using: .utf8) else { return [:] }

let anyResult: Any? = try? JSONSerialization.jsonObject(with: data, options: [])

return anyResult as? [String: String] ?? [:]

}

用法:

let string1 = "{\"City\":\"Paris\"}"

let dictionary1 = convertToDictionary(from: string1)

print(dictionary1) // prints: ["City": "Paris"]

let string2 = "{\"Quantity\":100}"

let dictionary2 = convertToDictionary(from: string2)

print(dictionary2) // prints: [:]

let string3 = "{\"Object\"}"

let dictionary3 = convertToDictionary(from: string3)

print(dictionary3) // prints: [:]

#4 . 使用不抛出并返回可选类型的方法

import Foundation

func convertToDictionary(from text: String) -> [String: String]? {

guard let data = text.data(using: .utf8) else { return nil }

let anyResult = try? JSONSerialization.jsonObject(with: data, options: [])

return anyResult as? [String: String]

}

用法:

let string1 = "{\"City\":\"Paris\"}"

let dictionary1 = convertToDictionary(from: string1)

print(String(describing: dictionary1)) // prints: Optional(["City": "Paris"])

let string2 = "{\"Quantity\":100}"

let dictionary2 = convertToDictionary(from: string2)

print(String(describing: dictionary2)) // prints: nil

let string3 = "{\"Object\"}"

let dictionary3 = convertToDictionary(from: string3)

print(String(describing: dictionary3)) // prints: nil

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值