简体   繁体   中英

How to convert NSArray of NSStrings into Json String iOS

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

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

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);
}
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];

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM