服务器端的 Gigya SDK 将数据表示为 JSON 对象,它能够表示键或数组下的嵌套对象。
对于帐户上的“profile.phone”属性,它存储为对象数组,如下所述:
{
"profile": {
"phones": [
{ "type": "phone", "number": "8005551234" },
{ "type": "cell", "number": "8885551234" }
]
}
}
通常,在使用 Gigya 的 iOS API 时,通常将这些 JSON 概念分别映射到 NSMutableDictionary 和 NSMutableArray 类,然后使用 NSJSONSerialization 类对数据进行序列化。
因此,例如,如果我们想在 Gigya 的帐户上设置电话号码,如上所示,那么您需要使用以下代码来完成此操作:
NSMutableDictionary *phone1 = [NSMutableDictionary dictionary];
[phone1 setObject:@"phone" forKey:@"type"];
[phone1 setObject:@"8005551234" forKey:@"number"];
NSMutableDictionary *phone2 = [NSMutableDictionary dictionary];
[phone2 setObject:@"cell" forKey:@"type"];
[phone2 setObject:@"8885551234" forKey:@"number"];
NSMutableArray *phones = [NSMutableArray array];
[phones addObject:phone1];
[phones addObject:phone2];
NSMutableDictionary *profile = [NSMutableDictionary dictionary];
[profile setObject:phones forKey:@"phones"];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:profile
options:0
error:&error];
GSRequest *request = [GSRequest requestForMethod:@"accounts.setAccountInfo"];
[request.parameters setObject:jsonString forKey:@"profile"];
[request sendWithResponseHandler:^(GSResponse *response, NSError *error) {
if (!error) {
NSLog(@"Success");
// Success! Use the response object.
}
else {
NSLog(@"error");
// Check the error code according to the GSErrorCode enum, and handle it.
}
}];
或者,您可以直接构造一个 JSON 字符串;但是对于添加新属性时需要进行的任何更改,上述策略往往更加灵活。