【问题标题】:NSDictionary structure not getting done properlyNSDictionary 结构未正确完成
【发布时间】:2018-04-18 11:07:54
【问题描述】:

我想使用 NSDictionary 制作如下结构

{
 "request":
    {
      "email":"manger@abc.com",
      "password":"manager"
    }
}

我在目标 C 中使用以下代码

  NSDictionary *paramsDic = [[NSDictionary alloc] initWithObjectsAndKeys:
                           @"manger@hall.com",@"email",
                           @"manager",@"password",
                           nil];


  NSDictionary *req = [[NSDictionary alloc] initWithObjectsAndKeys:
                           paramsDic,@"request",
                           nil];

但它返回以下响应

 {
   request =     
   {
      email = "manger@abc.com";
      password = manager;
   };
 }

它显示分号而不是逗号,一个额外的分号和键不在引号中

【问题讨论】:

  • 你的结构是JSON,你需要NSJSONSerialization。 SO上有数千个相关问题
  • 你正在调用NSDictionary-description方法,这就是iOS打印字典的方式。没什么不好的。现在,您的字典是 JSON Valid,您需要使用 NSJSONSerialization 将其转换为 NSData,如果需要,您可以将 NSData 转换为 NSString
  • 感谢接受:)

标签: ios objective-c json nsdictionary


【解决方案1】:

将req转换为JSON对象

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:req options:NSJSONWritingPrettyPrinted error:nil];
NSLog(@"%@", jsonData);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多