【问题标题】:How to convert NSArray of NSStrings into Json String iOS如何将 NSStrings 的 NSArray 转换为 Json String iOS
【发布时间】:2014-05-03 08:44:52
【问题描述】:

我有一个卷号数组

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

我需要在 Web 服务请求中发送这个数组

它的格式是这样的(JSON格式)

JSON 数据

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

但我在将滚动数字数组 (rollArray) 转换为 json 字符串时遇到问题 以所需的格式。

我正在尝试这个

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

我使用的是 iOS 7

【问题讨论】:

  • 你能添加导致问题的代码吗?还有什么问题?
  • NSDictionary 已通过 NSJSONSerialization 成功转换为 JSON 格式/从 JSON 格式转换。请检查我的答案。

标签: ios objective-c json cocoa nsarray


【解决方案1】:

您可以使用以下方法从任何类型的 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);

【讨论】:

    【解决方案2】:
    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];
    

    【讨论】:

      【解决方案3】:

      试试这个:

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

      【讨论】:

        【解决方案4】:

        无需使用以下代码sn-ps:

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

        您可以使用以下代码创建 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]);
        

        【讨论】:

        • 感谢您的回复。可能是我今天工作累了,我只是在这么简单的事情上多花心思。
        • @DineshKaushik:感谢您的评论。是时候喝点咖啡了:)
        猜你喜欢
        • 2019-03-28
        • 2017-11-29
        • 1970-01-01
        • 1970-01-01
        • 2016-08-05
        • 2012-02-24
        • 1970-01-01
        • 2019-08-26
        • 1970-01-01
        相关资源
        最近更新 更多