我有一个从NSJSONSerialization构建的json字符串,如下所示:
{
"credentialMemberControlled" : "",
"credentialRSVP" : 0,
"credentialIsCheckedIn" : 0,
"credentialMember" : {
"memberEMail" : "",
"memberJob" : "",
"memberCompany" : "",
"memberFirstName" : "test4",
"memberPhoneNumber" : "",
"memberLastName" : "test4"
},
"credentialTypeName" : "",
"credentialIsCheckedOut" : 0,
"credentialComment" : "",
"credentialHosts" : [
{
"hostName" : "testHostName"
}
],
"credentialCountCheckOut" : 0,
"credentialCountCheckIn" : 0,
"credentialId" : "2015081130008",
"credentialExtensionFields" : [
],
"credentialIsHost" : 0
}
它是由一个NSManaged类和Shoaib编写的解析器生成的。
(我没有使用Hearst ObjectMapper)从头开始映射我的NSManaged类及其关系。
问题:在等待儿子数据时,我需要更改所有的姓名,以适应具有不同键名的Web服务。
编辑1 :
所以我的NSManaged Class Credential看起来像这样:
import Foundation
import CoreData
class Credential: NSManagedObject, Mappable {
@NSManaged var credentialArrivalDate: String?
@NSManaged var credentialBarCode: String?
@NSManaged var credentialComment: String?
@NSManaged var credentialCountCheckIn: Int
@NSManaged var credentialCountCheckOut: Int
@NSManaged var credentialDepartureDate: String?
@NSManaged var credentialId: String?
@NSManaged var credentialIsArrived: Bool
@NSManaged var credentialIsCheckedOut: Bool
@NSManaged var credentialIsHost: Bool
@NSManaged var credentialLastModificationDate: String?
@NSManaged var credentialMemberControlled: String?
@NSManaged var credentialNumberOfPeople: Int
@NSManaged var credentialRSVP: Int
@NSManaged var credentialTypeId: String?
@NSManaged var credentialTypeName: String?
@NSManaged var credentialZoneId: String?
@NSManaged var credentialZoneName: String?
@NSManaged var credentialHosts: Set<Host>?
@NSManaged var credentialMember: Member
@NSManaged var credentialExtensionFields: Set<ExtensionFields>?
@NSManaged var credentialDeltaFirstCheckIn: String?
@NSManaged var credentialDeltaFirstCheckOut: String?
@NSManaged var credentialIsCheckedIn: Bool
}
我创建了一个新的Credential实体,如下所示:
// create Credential entity
let newCredential = NSEntityDescription.insertNewObjectForEntityForName("Credential", inManagedObjectContext: self.managedObjectContext!) as! Credential
然后我检索一些文本字段值并将其提供给我的newCredential:
// set credential values
newCredential.setValue(textFieldType.text, forKey: "credentialTypeName")
等
在某些时候,我通过其ID:
检索我的“当前正在创建的”凭据// create a request to fetch the credential
let requestGuest = NSFetchRequest(entityName: "Credential")
// set a predicate to retrieve the credential according to its Id
requestGuest.predicate = NSPredicate(format: "credentialId = %@", newCredential.credentialId!)
// execute request
let fetchedGuest = (managedObjectContext!.executeFetchRequest(requestGuest, error:nil))!
// convert fetched Guest to data
let dataInArr: NSArray = ManagedParser.convertToArray(fetchedGuest)
然后我可以使用NSJSONSerialization从该数组中获取JSON数据:
var jsonError : NSError?
jsonCredential = NSJSONSerialization.dataWithJSONObject(dataInArr, options: NSJSONWritingOptions.PrettyPrinted, error: &jsonError)
这就是我在原帖中打印结果的方式:
let stringJSONCredential = NSString(data: jsonCredential!, encoding: NSUTF8StringEncoding)
println(stringJSONCredential!)
我需要做的是:
现在我甚至陷入了字典解决方案,因为我没有本地字典类型来循环,只有一些NSData打印出来。
希望这篇文章更容易理解!
编辑2
这是在使用NSJSONSerialization之前应该映射的数组的(示例)内容,如@vadian所示:
(
{
credentialComment = "";
credentialCountCheckIn = 0;
credentialCountCheckOut = 0;
credentialExtensionFields = (
);
credentialHosts = (
);
credentialId = 2015081500280;
credentialMember = {
memberCompany = "";
memberEMail = "";
memberFirstName = test6;
memberJob = "";
memberLastName = test6;
memberPhoneNumber = "";
};
credentialMemberControlled = "";
credentialTypeName = "";
}
)
它仍然有点不完整,这意味着我有更多的键/值对可以添加,但你已经有了这个想法。
它包含的密钥是我需要更改以适应我的Web服务的密钥,这就是我被困住的地方。
再次感谢您的贡献
答案 0 :(得分:0)
这是一个例子。不是递归的,但你已经有了这个想法。
typealias SpecialDictionary = [String: AnyObject]
var dictionary: SpecialDictionary = [
"credentialMemberControlled" : "",
"credentialRSVP" : 0,
"credentialIsCheckedIn" : 0,
"credentialMember" : [
"memberEMail" : "",
"memberJob" : "",
"memberCompany" : "",
"memberFirstName" : "test4",
"memberPhoneNumber" : "",
"memberLastName" : "test4"
],
"credentialTypeName" : "",
"credentialIsCheckedOut" : 0,
"credentialComment" : "",
"credentialHosts" : [
[
"hostName" : "testHostName"
]
],
"credentialCountCheckOut" : 0,
"credentialCountCheckIn" : 0,
"credentialId" : "2015081130008",
"credentialExtensionFields" : [
],
"credentialIsHost" : 0
]
let keysMapping = [
"credentialMemberControlled": "memberControlled",
"credentialRSVP" : "rsvp",
"credentialIsCheckedIn" : "checkedIn",
]
func processDictionaryForWebservice(dictionary: SpecialDictionary) -> SpecialDictionary {
var result = SpecialDictionary()
for element in dictionary {
let value: AnyObject = element.1
let key = keysMapping[element.0] ?? element.0
result[key] = value
}
return result
}
答案 1 :(得分:0)
修改:考虑 EDIT 2 的问题
localizable.strings
。插入键/值对,左侧是当前值,右侧是新值,例如
"credentialCountCheckIn" = "countCheckIn";
"credentialCountCheckOut" = "countCheckOut;
使用此代码,假设dataInArr
包含一个字典数组,其中每个字典的键都必须映射。
var jsonError : NSError?
var resultArray = [Dictionary<String, AnyObject>]()
for aDictionary in dataInArr as! [Dictionary<String, AnyObject>] {
var modifiedDictionary = Dictionary<String, AnyObject>()
for (key, value) in aDictionary {
modifiedDictionary[NSLocalizedString(key, comment:"")] = value
}
resultArray.append(modifiedDictionary)
}
let jsonCredential = NSJSONSerialization.dataWithJSONObject(resultArray, options: NSJSONWritingOptions(), error: &jsonError)
如果在.strings文件中找不到匹配的密钥,则密钥不会更改