在我的项目中,有一个被声明为静态的包对象,当客户单击“批准订单”按钮时,我将包对象序列化为JSON字符串。目前没有问题。但是,当我打印序列化的json字符串时,所有与“Int”包中的“ID”转换为Bool键入json字符串结果。将自定义包对象序列化为json字符串int到Bool swift
详情如下过程相关的代码块:
这是我“序列化”类:
public class Serializable : NSObject
{
func toDictionary() -> NSDictionary
{
var aClass : AnyClass? = self.dynamicType
var propertiesCount : CUnsignedInt = 0
var propertiesInAClass : UnsafeMutablePointer<objc_property_t> = class_copyPropertyList(aClass, &propertiesCount)
var propertiesDictionary : NSMutableDictionary = NSMutableDictionary()
for(var i = 0; i < Int(propertiesCount); i++)
{
var property = propertiesInAClass[i]
var propName = NSString(CString: property_getName(property), encoding: NSUTF8StringEncoding)
var propType = property_getAttributes(property)
var propValue : AnyObject! = self.valueForKey(propName!)
if(propValue is Serializable)
{
propertiesDictionary.setValue((propValue as Serializable).toDictionary(), forKey: propName!)
}
else if(propValue is Array<Serializable>)
{
var subArray = Array<NSDictionary>()
for item in (propValue as Array<Serializable>)
{
subArray.append(item.toDictionary())
}
propertiesDictionary.setValue(subArray, forKey: propName!)
}
else if(propValue is NSData)
{
propertiesDictionary.setValue((propValue as NSData).base64EncodedStringWithOptions(nil), forKey: propName!)
}
else if(propValue is Bool)
{
propertiesDictionary.setValue((propValue as Bool).boolValue, forKey: propName!)
}
else if(propValue is NSDate)
{
var date = propValue as NSDate
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "Z"
var dateString = NSString(format: "/Date(%.0f000%@)/", date.timeIntervalSince1970, dateFormatter.stringFromDate(date))
propertiesDictionary.setValue(dateString, forKey: propName!)
}
else
{
propertiesDictionary.setValue(propValue, forKey: propName!)
}
}
return propertiesDictionary
}
func toJson() -> NSData!
{
var dictionary = self.toDictionary()
var err: NSError?
return NSJSONSerialization.dataWithJSONObject(dictionary, options:NSJSONWritingOptions(0), error: &err)
}
func toJsonString() -> NSString!
{
return NSString(data: self.toJson(), encoding: NSUTF8StringEncoding)
}
override init()
{
}
}
这是我BagItem类:
class BagItem: Serializable, Hashable {
var uniqueID: Int = 0
override var hashValue: Int { return uniqueID.hashValue }
var bagItemId: String
var item: Item
var boughtDate: NSDate!
var boughtTime: String
var branch: Branch
var isMainItem: Bool
override init()
{
self.bagItemId = ""
self.item = Item()
self.boughtDate = NSDate()
self.boughtTime = ""
self.branch = Branch()
self.isMainItem = false
}
}
func ==(lhs: BagItem, rhs: BagItem) -> Bool
{
return lhs.uniqueID == rhs.uniqueID
}
这是我的“SerializationBag” class:
class SerializableBag: Serializable
{
var bag: Array<BagItem> = []
override init()
{
}
}
这是我“ConvertBagToJSON”方法,袋类:如下
static func ConvertBagToJson() -> NSString
{
var serializer: SerializableBag = SerializableBag()
serializer.bag = self.bag
return serializer.toJsonString()
}
而且我回来JSON字符串结果:
{
"bag": [
{
"branch": {
"city": {
"cityId": false,
"cityName": ""
},
"town": {
"townName": "",
"townId": false
},
"branchName": "Branch",
"branchId": true,
"business": {
"businessPhotoPath": "",
"businessName": "",
"businessId": true
},
"branchAddress": "Some Address",
"branchTelephone": ""
},
"uniqueID": false,
"boughtDate": "/Date(1414581909000+0200)/",
"item": {
"itemName": "Pencil",
"itemId": true,
"itemPrice": true
},
"isMainItem": true,
"bagItemId": "9674D47B-0D2F-46CC-BA16-754875AE277D",
"hashValue": false,
"boughtTime": "00:30"
}
]
}
正如你看到的,在JSON字符串,ID为布尔,但他们必须在Int类型。我怎么解决这个问题 ?
谢谢你的答案
问候
这是非常有趣的。有没有其他解决方案来做到这一点? @印记 – user2174358 2014-10-29 13:55:50
对不起,但我删除了评论。我会写解决方案。 – rintaro 2014-10-29 13:56:57