【问题标题】:Converting an NSArray of Dictionaries to JSON array in iOS在 iOS 中将字典的 NSArray 转换为 JSON 数组
【发布时间】:2012-08-31 06:23:49
【问题描述】:

我需要以JSON 数组格式向服务器发送NSArray。如何将其转换为JSON。这是我必须通过的NSArray 的示例。

array([0] => array('latitude'=>'10.010490', 
                  'longitude'=>'76.360779', 
                   'altitude'=>'30.833334', 
                  'timestamp'=>'11:17:23', 
                      'speed'=>'0.00', 
                   'distance'=>'0.00');

[1] => array('latitude'=>'10.010688', 
            'longitude'=>'76.361378', 
             'altitude'=>'28.546305', 
            'timestamp'=>'11:19:26', 
                'speed'=>'1.614', 
             'distance'=>'198.525711')
 )`

而且需要的格式是这样的

[
  { "latitude":"10.010490",
   "longitude":"76.360779",
    "altitude":"30.833334",
   "timestamp":"11:17:23", 
       "speed":"0.00",
    "distance":"0.00"
  },    
  {
   "latitude":"10.010688",
  "longitude":"76.361378",
   "altitude":"28.546305",
  "timestamp":"11:19:26",
      "speed":"1.614",
   "distance":"198.525711" 
  }
]

有人有解决办法吗?提前致谢。

【问题讨论】:

    标签: iphone json cocoa cocoa-touch nsarray


    【解决方案1】:

    最简单最好的方法!!!

    要将 NSArray 或 NSMutableArray 转换为 jsonString,您可以先将其转换为 NSData,然后再将其转换为 NSString。使用此代码

    NSData* data = [ NSJSONSerialization dataWithJSONObject:yourArray options:NSJSONWritingPrettyPrinted error:nil ];
    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    

    它帮助了我,希望它也能帮助你。一切顺利。

    【讨论】:

    • 这是题名的正确答案。谢谢。接受的答案显示了一系列字典
    【解决方案2】:

    查看this 教程,iOS 5.0 中的 JSON 解释清楚(serailization、deserailization)。

    【讨论】:

      【解决方案3】:

      您调用的服务是 RESTful 服务吗?

      如果是这样,我强烈建议使用 RestKit。它进行对象序列化/反序列化。它还处理所有网络基础。非常有价值,并且维护良好。

      【讨论】:

      • 为这样一个简单的任务使用框架并不是一个好主意。 2016 年更新 RestKit 不再是一个维护框架。
      【解决方案4】:
      NSDictionary *firstJsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @"10.010490", @"latitude",
                                  @"76.360779", @"longitude",
                                  @"30.833334", @"altitude",
                                  @"11:17:23", @"timestamp",
                                  @"0.00", @"speed",
                                  @"0.00", @"distance",
                                  nil];
      
        NSDictionary *secondJsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @"10.010490", @"latitude",
                                  @"76.360779", @"longitude",
                                  @"30.833334", @"altitude",
                                  @"11:17:23", @"timestamp",
                                  @"0.00", @"speed",
                                  @"0.00", @"distance",
                                  nil];
      
      NSMutableArray * arr = [[NSMutableArray alloc] init];
      
      [arr addObject:firstJsonDictionary];
      [arr addObject:secondJsonDictionary];
      
      NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
      NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
      NSLog(@"jsonData as string:\n%@", jsonString);
      

      【讨论】:

      • 它对我有用,但我希望以数组格式而不是字符串格式响应。
      • inline 'code [ { "latitude":"10.010490", "longitude":"76.360779", "altitude":"30.833334", "timestamp":"11:17:23", "速度”:“0.00”,“距离”:“0.00”},{“纬度”:“10.010688”,“经度”:“76.361378”,“高度”:“28.546305”,“时间戳”:“11:19: 26", "speed":"1.614", "distance":"198.525711" } ] ' 你只会得到数组格式
      【解决方案5】:

      我会推荐SBJson-Framework

      转换NSMutableArray 就像NSString *jsonString = [yourArray JSONRepresentation]; 一样简单

      编辑:Jack Farnandish 是对的,您必须先将其转换为 NSDictionary,然后才能将其转换为 Json。在我的示例中,NSMutableArray 必须包含字典。数组只需要在字符串的开头和结尾创建方括号。

      【讨论】:

        【解决方案6】:
        #import <JSONKit/JSON.h>
        
        NSArray *array = // Your array here.
        NSString *json = [array JSONString];
        
        NSLog(@"%@", json);
        

        在我自己和作者的基准测试中,JSONKit 的性能明显优于 SBJson 和其他人。

        【讨论】:

          【解决方案7】:

          首先您必须将结构更改为NSDictionary 类和包含NSDictionary 对象的NSArray,然后在iOS 5 中尝试JSONKit 序列化比标准NSJSONSerialization 效果更好。

          【讨论】:

            【解决方案8】:

            您可以使用 iOS 的 build in JSON functions 或使用外部库,例如JSONKit 将您的数据转换为 JSON

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2019-04-19
              • 2012-02-24
              • 1970-01-01
              • 2023-03-15
              • 2020-01-13
              • 2018-05-04
              相关资源
              最近更新 更多