- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在解析 json 时遇到错误
"hoursOfOperation" : {
"sat" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1020,
"open" : 600
}
}
]
},
"fri" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1260,
"open" : 660
}
}
]
},
"sun" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1020,
"open" : 600
}
}
]
},
"mon" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1260,
"open" : 960
}
}
]
},
"tue" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1260,
"open" : 660
}
}
]
},
"wed" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1260,
"open" : 660
}
}
]
},
"thu" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1260,
"open" : 660
}
}
]
}
}
这是 JSON 共享。我正在使用 JSONSerialization 来解析它。代码如下:
struct HoursOfOperation{
var sun : hoursOfOperationData?
var mon : hoursOfOperationData?
var tue : hoursOfOperationData?
var wed : hoursOfOperationData?
var thu : hoursOfOperationData?
var fri : hoursOfOperationData?
var sat : hoursOfOperationData?
init(_ info: AnyObject) {
let s = String(describing: info)
let data = s.data(using: String.Encoding.utf8, allowLossyConversion: false)!
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String: AnyObject]
if let sun = json["sun"]{
self.sun = hoursOfOperationData.init(sun)
}
if let sat = json["sat"]{
self.sat = hoursOfOperationData.init(sat)
}
if let fri = json["fri"]{
self.fri = hoursOfOperationData.init(fri)
}
if let thu = json["thu"] {
self.thu = hoursOfOperationData.init(thu)
}
if let wed = json["wed"]{
self.wed = hoursOfOperationData.init(wed)
}
if let tue = json["tue"]{
self.tue = hoursOfOperationData.init(tue)
}
if let mon = json["mon"] {
self.mon = hoursOfOperationData.init(mon)
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
}
}
//hoursOfOperationData
struct hoursOfOperationData{
var enabled : AnyObject?
var schedule : [scheduleData]?
init(_ info: AnyObject) {
let s = String(describing: info)
let data = s.data(using: String.Encoding.utf8, allowLossyConversion: false)!
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String: AnyObject]
if let enabled = json["enabled"]{
self.enabled = enabled as AnyObject
}
if let schedule = json["schedule"] as? NSArray{
for dic in schedule{
schedule.adding(scheduleData.init(dic as AnyObject))
}
self.schedule = schedule as? [scheduleData]
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
}
}
//调度数据
struct scheduleData{
var time : scheduleDataForLocation?
init(_ info: AnyObject) {
let s = String(describing: info)
let data = s.data(using: String.Encoding.utf8, allowLossyConversion: false)!
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String: AnyObject]
if let time = json["time"]{
self.time = scheduleDataForLocation.init(time)
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
}
}
//scheduleDataForLocation
struct scheduleDataForLocation{
var openTime : AnyObject?
var closeTime : AnyObject?
init(_ info: AnyObject) {
let s = String(describing: info)
let data = s.data(using: String.Encoding.utf8, allowLossyConversion: false)!
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String: AnyObject]
if let open = json["open"]{
self.openTime = open as AnyObject
}
if let close = json["close"]{
self.closeTime = close as AnyObject
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
}
}
在准备此模型时,我无法解析 json 并收到错误消息
' Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around character 6." UserInfo={NSDebugDescription=No string key for value in object around character 6.} '
请提出正确的处理方法。我访问过许多堆栈溢出问题和答案,但没有一个能回答我的问题。任何帮助将不胜感激。
最佳答案
您可以直接使用下面的模型类,引用:http://www.jsoncafe.com/
class OperationModel : NSObject, NSCoding{
var hoursOfOperation : HoursOfOperation!
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
init(fromDictionary dictionary: [String:Any]){
if let hoursOfOperationData = dictionary["hoursOfOperation"] as? [String:Any]{
hoursOfOperation = HoursOfOperation(fromDictionary: hoursOfOperationData)
}
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if hoursOfOperation != nil{
dictionary["hoursOfOperation"] = hoursOfOperation.toDictionary()
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
@objc required init(coder aDecoder: NSCoder)
{
hoursOfOperation = aDecoder.decodeObject(forKey: "hoursOfOperation") as? HoursOfOperation
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
@objc func encode(with aCoder: NSCoder)
{
if hoursOfOperation != nil{
aCoder.encode(hoursOfOperation, forKey: "hoursOfOperation")
}
}
}
class Schedule : NSObject, NSCoding{
var time : Time!
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
init(fromDictionary dictionary: [String:Any]){
if let timeData = dictionary["time"] as? [String:Any]{
time = Time(fromDictionary: timeData)
}
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if time != nil{
dictionary["time"] = time.toDictionary()
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
@objc required init(coder aDecoder: NSCoder)
{
time = aDecoder.decodeObject(forKey: "time") as? Time
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
@objc func encode(with aCoder: NSCoder)
{
if time != nil{
aCoder.encode(time, forKey: "time")
}
}
}
class HoursOfOperation : NSObject, NSCoding{
var fri : Day!
var mon : Day!
var sat : Day!
var sun : Day!
var thu : Day!
var tue : Day!
var wed : Day!
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
init(fromDictionary dictionary: [String:Any]){
if let friData = dictionary["fri"] as? [String:Any]{
fri = Day(fromDictionary: friData)
}
if let monData = dictionary["mon"] as? [String:Any]{
mon = Day(fromDictionary: monData)
}
if let satData = dictionary["sat"] as? [String:Any]{
sat = Day(fromDictionary: satData)
}
if let sunData = dictionary["sun"] as? [String:Any]{
sun = Day(fromDictionary: sunData)
}
if let thuData = dictionary["thu"] as? [String:Any]{
thu = Day(fromDictionary: thuData)
}
if let tueData = dictionary["tue"] as? [String:Any]{
tue = Day(fromDictionary: tueData)
}
if let wedData = dictionary["wed"] as? [String:Any]{
wed = Day(fromDictionary: wedData)
}
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if fri != nil{
dictionary["fri"] = fri.toDictionary()
}
if mon != nil{
dictionary["mon"] = mon.toDictionary()
}
if sat != nil{
dictionary["sat"] = sat.toDictionary()
}
if sun != nil{
dictionary["sun"] = sun.toDictionary()
}
if thu != nil{
dictionary["thu"] = thu.toDictionary()
}
if tue != nil{
dictionary["tue"] = tue.toDictionary()
}
if wed != nil{
dictionary["wed"] = wed.toDictionary()
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
@objc required init(coder aDecoder: NSCoder)
{
fri = aDecoder.decodeObject(forKey: "fri") as? Day
mon = aDecoder.decodeObject(forKey: "mon") as? Day
sat = aDecoder.decodeObject(forKey: "sat") as? Day
sun = aDecoder.decodeObject(forKey: "sun") as? Day
thu = aDecoder.decodeObject(forKey: "thu") as? Day
tue = aDecoder.decodeObject(forKey: "tue") as? Day
wed = aDecoder.decodeObject(forKey: "wed") as? Day
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
@objc func encode(with aCoder: NSCoder)
{
if fri != nil{
aCoder.encode(fri, forKey: "fri")
}
if mon != nil{
aCoder.encode(mon, forKey: "mon")
}
if sat != nil{
aCoder.encode(sat, forKey: "sat")
}
if sun != nil{
aCoder.encode(sun, forKey: "sun")
}
if thu != nil{
aCoder.encode(thu, forKey: "thu")
}
if tue != nil{
aCoder.encode(tue, forKey: "tue")
}
if wed != nil{
aCoder.encode(wed, forKey: "wed")
}
}
}
class Time : NSObject, NSCoding{
var close : Int!
var open : Int!
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
init(fromDictionary dictionary: [String:Any]){
close = dictionary["close"] as? Int
open = dictionary["open"] as? Int
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if close != nil{
dictionary["close"] = close
}
if open != nil{
dictionary["open"] = open
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
@objc required init(coder aDecoder: NSCoder)
{
close = aDecoder.decodeObject(forKey: "close") as? Int
open = aDecoder.decodeObject(forKey: "open") as? Int
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
@objc func encode(with aCoder: NSCoder)
{
if close != nil{
aCoder.encode(close, forKey: "close")
}
if open != nil{
aCoder.encode(open, forKey: "open")
}
}
}
class Day : NSObject, NSCoding{
var enabled : Bool!
var schedule : [Schedule]!
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
init(fromDictionary dictionary: [String:Any]){
enabled = dictionary["enabled"] as? Bool
schedule = [Schedule]()
if let scheduleArray = dictionary["schedule"] as? [[String:Any]]{
for dic in scheduleArray{
let value = Schedule(fromDictionary: dic)
schedule.append(value)
}
}
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if enabled != nil{
dictionary["enabled"] = enabled
}
if schedule != nil{
var dictionaryElements = [[String:Any]]()
for scheduleElement in schedule {
dictionaryElements.append(scheduleElement.toDictionary())
}
dictionary["schedule"] = dictionaryElements
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
@objc required init(coder aDecoder: NSCoder)
{
enabled = aDecoder.decodeObject(forKey: "enabled") as? Bool
schedule = aDecoder.decodeObject(forKey: "schedule") as? [Schedule]
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
@objc func encode(with aCoder: NSCoder)
{
if enabled != nil{
aCoder.encode(enabled, forKey: "enabled")
}
if schedule != nil{
aCoder.encode(schedule, forKey: "schedule")
}
}
}
使用:
let jsonData = try Data(contentsOf: url)
let json = try JSONSerialization.jsonObject(with: jsonData) as! [String : Any]
let arrOper = OperationModel(fromDictionary: json)
输出:
关于ios - JSONSerialization 解析问题 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54819621/
问题 我有一个 Spring MVC 应用程序,它要求我将某个实体列表的 id 和名称转换为具有特定格式的 JSON 对象数组,并在某个请求上输出。也就是说,我需要一个像这样的 JSON 对象数组:
当我阅读最新 RestSharp 的 readme.txt 时: *** IMPORTANT CHANGE IN RESTSHARP VERSION 103 *** In 103.0, JSON.NE
我有两个类(class)。以二等领域第一。 class A { @JsonSerializer(using = CustomBSerializer.class) private B b; }
有什么方法可以从 JsonSerializer 类中取出设置并在新的 JsonSerializer 中重新实现它们? 似乎没有任何方法可以做那样的事情。我发现最好的是通过反射调用的私有(private
在我的java应用程序中,我分别使用 JsonSerializer 和 JsonSerializer() 来序列化 java.util.Date 和 java.sql.Timestamp 字段。但问题
我想使用 Foundation 的 JSONSerialization.data(withJSONObject obj: AnyObject, options opt:writingOptions =
根据PHP手册 If a Pure Enum is serialized to JSON, an error will be thrown. If a Backed Enum is serialize
我有一个父类,我在其上指定了一个像这样的自定义反序列化器 - @JsonDeserialize(using = CustomDeserializer.class) public class Paren
根据PHP手册 If a Pure Enum is serialized to JSON, an error will be thrown. If a Backed Enum is serialize
我有一个位于 .jar 中的类,但无法修改。 它在类上有一个 @JsonSerialize() 注释。 @JsonSerialize(using = SomeClassSerializer.class
我正在使用 JSON token 开发 iOS,但遇到了 unsupported_grant_type 错误。我正在开发的代码如下 let params = ["grant_type": "passw
我有以下代码将 joda 的 LocalDate 序列化为字符串: public class JodaDateSerializer extends JsonSerializer { priva
我想添加一个包装器,其命名是在运行时确定的,因为它取决于类名(我可以使用@JsonRootName,但我不想这样做,因为我必须在每个子类上使用它,即效率不高)。 我想我应该使用 @JsonSerial
我编写了一个自定义的 JsonSerializer 来将 BigDecimal 转换为 String。我想使用 @JsonSerialize 注释调用此序列化程序,但有条件地,即如果特定 boolea
JSONSerialization 未在服务器发送时序列化数据。它反转数据。我使用后端的数据过滤 API。它发送准确的数据,我还检查了 postman 和 Android 端,但 iOS 代码更改了响
我正在尝试将 SwiftyJSON 与我的 JSON api 数据一起使用,但我无法理解转换的工作原理。 这是 SwiftyJSON Git 的链接:https://github.com/Swifty
我有一个自定义类 public class Balance: NSObject { var details: String var date: Date var amount:
我在解析 json 时遇到错误 "hoursOfOperation" : { "sat" : { "enabled" : true, "schedule"
什么是 option: [] in JSONSerialization in swift let jsonData = try JSONSerialization.data(withJSONObjec
我在将我的模型翻译成字典时遇到了问题。我的模型继承自 Codable,当使用 JSONSerialization 时,我得到了一个字典,其形式为: (...,"sensors": ( { accx =
我是一名优秀的程序员,十分优秀!