我想在API调用中发送以下JSON参数,但下面使用的contacts数组字符串混乱,我无法在iOS中形成它。下面是在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;
}
Framed error 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"
}
我在控制台中遇到的错误是,
值__NSCFConstantString * @“JSON文本没有以数组或对象和选项开头,以允许未设置片段。” 0x000000010cf2ed50
答案 0 :(得分:1)
这是一种可以将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"];