如何在ios中发送多个参数?

时间:2016-05-03 04:39:13

标签: 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"];

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

4 个答案:

答案 0 :(得分:2)

只需使用以下代码..

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"];

答案 1 :(得分:1)

你可以这样做,

NSDictionary *dict1 = @{@"LoginID":@"151",@"UserID":@"0",@"SubUserID":@"0",@"WorkGroupID":@"92",@"WorksFor":@"Doctor",@"UserWorkGroup":@"0"};

NSDictionary *dict2 = @{@"SortingName":@",",@"Searching":@"true",@"SortingOrder":@"Desc",@"RecordsCount":@"10",@"PageIndex":@"0"};


NSArray *arr = [NSArray arrayWithObjects:dict1,dict2, nil];

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

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

希望这会有所帮助:)

答案 2 :(得分:0)

将其添加到NSMutableArray并将该数组添加到NSDictionary。等;

{"LoginID":151,"UserID":0,"SubUserID":0,"WorkGroupID":92,"WorksFor":"Doctor","UserWorkGroup":0}

此内容为NSMutableArrayNSMutableArrayNSDictionary

答案 3 :(得分:0)

数组放入词典的简单方法是

var selected = listBox1.SelectedItem as Account;
if (selected != null)
; // use it