Josh Kethepalli
Josh Kethepalli

Reputation: 125

how to send an array as a parameter to json service in iOS

I am using iPhone JSON Web Service based app.I need to pass input parameter as an array to a JSON web Service, how can I do this?

Array Contains 12 elements. Here am providing sample service... input parametes for this service: dev_id = 1; dev_name= josh and array items (projectslist,companyidentifier)

http://www.jyoshna.com/api/developer.php?dev_id=1&dev_name=josh&(Here i need to pass the array elements)

can any help us how to pass array as a input parameter to the json service?

Upvotes: 0

Views: 1819

Answers (3)

Adam Richardson
Adam Richardson

Reputation: 2526

you will need to create an NSMutabelDictionary of your array then JSON encode it, you can then send the resulting string you your webservice however you choose. I tend to build a POST request and send it that way

NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *tagData = [[NSMutableDictionary alloc] init];
for(int i = 0; i < array.count; i++)
{
    NSString *keyString = [NSString stringWithFormat:@"key%i", i];
    [tagData setObject:[array objectAtIndex:i] forKey:keyString];
}

[jsonDict setObject:tagData forKey:@"entries"];

NSData* data = [NSJSONSerialization dataWithJSONObject:jsonDict
                                               options:NSJSONWritingPrettyPrinted error:&error];

NSString* aStr;
aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

It is the sgtring aStr that you need to send

Upvotes: 0

manujmv
manujmv

Reputation: 6445

You have to serialize the array and pass as an argument. Dont forget to unserialize in server side

Upvotes: 0

NSLog
NSLog

Reputation: 649

First you have to convert array as JSON string

NSString *requestString=[jsonParser stringWithObject:array];

convert string to data

NSData *data=[requestString dataUsingEncoding:NSUTF8StringEncoding];

set that data as request Body

[request setHTTPBody:data];

Upvotes: 1

Related Questions