无法从 iOS 中的 NSArray 转换为正确格式的 NSString
Unable to convert to a proper format of NSString from NSArray in iOS
我想在 API 调用中发送以下 JSON 参数,但下面使用的联系人数组字符串令人困惑,我无法在 [=25= 中形成它].下面是在 Rest 客户端中测试的工作 JSON 参数。如何在 iOS?
中形成包含联系人数组的类似字符串模式
工作JSON参数,
{
"contacts": "[\"5555228243\",\"919677012480\"]",
"phno": "919791871448",
"device": "iphone",
"key": "key",
"name": "Logunath Subramaniyan",
"files": "files"
}
我下面的转换代码,
NSMutableDictionary *reqData = [[NSMutableDictionary alloc]init];
[reqData setObject:[FMCoredDataUtility fetchDetailForKey:kmobileNumber] forKey:@"phno"];
[reqData setObject:[FMCoredDataUtility fetchUserNameForKey:kuserName ]forKey:@"name"];
[reqData setObject:@"iphone" forKey:@"device"];
[reqData setObject:@"key" forKey:@"key"];
[reqData setObject:[self getMobileContacts ] forKey:@"contacts"];
[reqData setObject:@"files" forKey:@"files"];
-(NSArray*)getMobileContacts{
contactNumbers = [addUtility getContactNumbers];
for ( int i = 0; i < [contactNumbers count]; i++) {
[filteredContacts addObject:[[[contactNumbers objectAtIndex:i] componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""]];
}
return filteredContacts;
}
帧错误JSON参数,
{
"contacts": [
"5555228243",
"5554787672",
"4085555270",
"4085553514",
"5556106679",
"5557664823",
"7075551854",
"8885555512",
"8885551212",
"5555648583",
"4155553695",
"919677012480"
],
"phno": "919791871448",
"device": "iphone",
"key": "key",
"name": "Logunath Subramaniyan",
"files": "files"
}
我在控制台中遇到的错误是,
value __NSCFConstantString *
@"JSON text did not start with array or object and option to allow fragments not set."
0x000000010cf2ed50
这里有一种方法可以将 ios 数组转换为 JSON 字符串
NSArray *contactsArray = [self getMobileContacts ];//This will be your contacts array
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myArray options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[reqData setObject:jsonString forKey:@"contacts"];
我想在 API 调用中发送以下 JSON 参数,但下面使用的联系人数组字符串令人困惑,我无法在 [=25= 中形成它].下面是在 Rest 客户端中测试的工作 JSON 参数。如何在 iOS?
中形成包含联系人数组的类似字符串模式工作JSON参数,
{
"contacts": "[\"5555228243\",\"919677012480\"]",
"phno": "919791871448",
"device": "iphone",
"key": "key",
"name": "Logunath Subramaniyan",
"files": "files"
}
我下面的转换代码,
NSMutableDictionary *reqData = [[NSMutableDictionary alloc]init];
[reqData setObject:[FMCoredDataUtility fetchDetailForKey:kmobileNumber] forKey:@"phno"];
[reqData setObject:[FMCoredDataUtility fetchUserNameForKey:kuserName ]forKey:@"name"];
[reqData setObject:@"iphone" forKey:@"device"];
[reqData setObject:@"key" forKey:@"key"];
[reqData setObject:[self getMobileContacts ] forKey:@"contacts"];
[reqData setObject:@"files" forKey:@"files"];
-(NSArray*)getMobileContacts{
contactNumbers = [addUtility getContactNumbers];
for ( int i = 0; i < [contactNumbers count]; i++) {
[filteredContacts addObject:[[[contactNumbers objectAtIndex:i] componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""]];
}
return filteredContacts;
}
帧错误JSON参数,
{
"contacts": [
"5555228243",
"5554787672",
"4085555270",
"4085553514",
"5556106679",
"5557664823",
"7075551854",
"8885555512",
"8885551212",
"5555648583",
"4155553695",
"919677012480"
],
"phno": "919791871448",
"device": "iphone",
"key": "key",
"name": "Logunath Subramaniyan",
"files": "files"
}
我在控制台中遇到的错误是,
value __NSCFConstantString * @"JSON text did not start with array or object and option to allow fragments not set." 0x000000010cf2ed50
这里有一种方法可以将 ios 数组转换为 JSON 字符串
NSArray *contactsArray = [self getMobileContacts ];//This will be your contacts array
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myArray options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[reqData setObject:jsonString forKey:@"contacts"];