ios - ios如何发送多个参数?

标签 ios objective-c iphone json

如何发送json格式的参数?

以下是我的输入参数:

[
    {
        "LoginID":151,
        "UserID":0,
        "SubUserID":0,
        "WorkGroupID":92,
        "WorksFor":"Doctor",
        "UserWorkGroup":0
    },
    {
        "SortingName":"",
        "Searching":true,
        "SortingOrder":"Desc",
        "RecordsCount":10,
        "PageIndex":0
    }
]

之前我发送的参数如下:

[getProfileServices sendSynchronousPostRequestWithStringForAction:getProfileURL andParameters:[[NSDictionary alloc] initWithObjectsAndKeys:@"55",@"LoginID",@"0",@"UserID",@"0",@"SubUserID",@"90",@"WorkGroupID",@"Doctor",@"WorksFor",@"0",@"UserWorkGroup",nil] andRequestType:@"POST"];

如何像这样发送,但现在我需要添加上面提到的更多输入?

最佳答案

只需使用以下代码..

NSMutableArray * myArray = [[NSMutableArray alloc]init];

NSMutableDictionary * myData = [[NSMutableDictionary alloc]init];
[myData setValue:@"151" forKey:@"LoginID"];
[myData setValue:@"0" forKey:@"UserID"];
[myData setValue:@"0" forKey:@"SubUserID"];
[myData setValue:@"92" forKey:@"WorkGroupID"];
[myData setValue:@"Doctor" forKey:@"WorksFor"];
[myData setValue:@"0" forKey:@"UserWorkGroup"];

[myArray addObject:myData];

myData = [[NSMutableDictionary alloc]init];
[myData setValue:@"," forKey:@"SortingName"];
[myData setValue:@"1" forKey:@"Searching"];
[myData setValue:@"Desc" forKey:@"SortingOrder"];
[myData setValue:@"10" forKey:@"RecordsCount"];
[myData setValue:@"0" forKey:@"PageIndex"];
[myArray addObject:myData];

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:myArray options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
NSLog(@"Your final jsonData as string:\n%@", jsonString);

如果您想将此数据发送到服务器,那么您可以使用以下代码:

NSData *dataToSend = [NSJSONSerialization dataWithJSONObject:myArray options:0 error:nil];

[getProfileServices sendSynchronousPostRequestWithStringForAction:getProfileURL andParameters:[[NSDictionary alloc] initWithObjectsAndKeys:dataToSend] andRequestType:@"POST"];

关于ios - ios如何发送多个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36995718/

相关文章:

ios - 如何计算标签的最后一行宽度?

iphone - 将日期转换为 UTC 格式

ios TDD 与 Kiwi、OCHamcrest 和 OCMockiti,它们可以混合使用吗?

ios - 多个 Storyboard : how small should they be?

objective-c - 如何在 objective-c 中声明全局变量

ios - NSPredicate 用于获取具有特定类(class)名称的所有学生?

iPhone SDK : Image scaling problem

iphone - 如何在 Xcode 5 中将新项目添加到源代码管理?

ios - 使用 JSON 的 uitableview 部分出现问题

ios - 访问包装在单独类中的静态方法中的 NSURLSessionTasks 的委托(delegate)