简体   繁体   English

如何将NSStrings的NSArray转换为Json String iOS

[英]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 我需要在Web服务请求中发送此数组

whose format is like this (in JSON format) 其格式如下(采用JSON格式)

JSON data JSON数据

{
  "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. 但我在将数字滚动数字(rollArray)转换为所需格式的json String时遇到问题。

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 我使用的是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: 您可以使用以下代码创建JSON:

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获取Json字符串:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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