3

I have an Array of Roll Numbers

NSArray *rollArray = [NSArray arrayWithObjects:@"1", @"22", @"24", @"11", nil];

I need to send this array in a Web Service request

whose format is like this (in JSON format)

JSON data

{
  "existingRoll":["22","34","45","56"], // Array of roll numbers
  "deletedRoll":["20","34","44","56"] // Array of roll numbers
}

but I am facing problem in converting Array of Roll numbers (rollArray) into json String in the desired format.

I am trying this

 NSMutableDictionary *postDict = [[NSMutableDictionary alloc]init];
  [postDict setValue:[rollArray componentsJoinedByString:@","] forKey:@"existingRoll"];

 NSString *str = [Self convertToJSONString:postDict]; // converts to json string

 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:str options:0 error:nil];

 [request setHTTPBody:jsonData];

I am using iOS 7

2
  • 1
    Can you add the code that causing the problem ? And what is the problem ? Commented Mar 25, 2014 at 13:15
  • NSDictionary is successfully converted to/from JSON format with NSJSONSerialization. Check my answer please. Commented Mar 25, 2014 at 13:22

4 Answers 4

10

There is no need to use the following code snippets:

  1. [rollArray componentsJoinedByString:@","]
  2. NSString *str = [Self convertToJSONString:postDict];

You can create JSON by using the following code:

NSArray *rollArray = [NSArray arrayWithObjects:@"1", @"22", @"24", @"11", nil];
NSMutableDictionary *postDict = [[NSMutableDictionary alloc]init];
[postDict setValue:rollArray forKey:@"existingRoll"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil];

// Checking the format
NSLog(@"%@",[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply. May be i am tired of working today I was just applying extra mind on such simple thing.
@DineshKaushik: Thanks for the comment. Time to drink some coffee :)
2

Try this :

NSDictionary *object = @{
    @"existingRoll":@[@"22",@"34",@"45",@"56"],
    @"deletedRoll":@[@"20",@"34",@"44",@"56"]
};

if ([NSJSONSerialization isValidJSONObject:object]) {
    NSData* data = [ NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:nil ];
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(str);
}

Comments

0
NSMutableDictionary *postDict = [[NSMutableDictionary alloc] init];

    [postDict setValue:@"Login" forKey:@"methodName"];
    [postDict setValue:@"admin" forKey:@"username"];
    [postDict setValue:@"12345" forKey:@"password"];
    [postDict setValue:@"mobile" forKey:@"clientType"];


    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil];

    // Checking the format
    NSString *urlString =  [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];


    // Convert your data and set your request's HTTPBody property
    NSString *stringData = [[NSString alloc] initWithFormat:@"jsonRequest=%@", urlString];

    //@"jsonRequest={\"methodName\":\"Login\",\"username\":\"admin\",\"password\":\"12345\",\"clientType\":\"web\"}";

    NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];

Comments

0

You can use following method to get Json string from any type of NSArray :

NSArray *rollArray = [NSArray arrayWithObjects:@"1", @"22", @"24", @"11", nil];

NSData *data = [NSJSONSerialization dataWithJSONObject:rollArray options:0 error:nil];

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

NSLog(@"Json string is: %@", jsonString);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.