【问题标题】:covert json into dictionary in ios在ios中将json转换为字典
【发布时间】:2026-01-05 12:40:01
【问题描述】:

我正在尝试将此 json 数据转换为字典,但我无法帮助我。

 {
     "homeMobileCountryCode": 310,
     "homeMobileNetworkCode": 260,
     "radioType": "gsm",
     "carrier": "T-Mobile",
     "cellTowers": [
      {
       "cellId": 39627456,
       "locationAreaCode": 40495,
       "mobileCountryCode": 310,
       "mobileNetworkCode": 260,
       "age": 0,
       "signalStrength": -95
      }
     ],
     "wifiAccessPoints": [
      {
       "macAddress": "01:23:45:67:89:AB",
       "signalStrength": 8,
       "age": 0,
       "signalToNoiseRatio": -65,
       "channel": 8
      },
      {
       "macAddress": "01:23:45:67:89:AC",
       "signalStrength": 4,
       "age": 0
      }
     ]
    }

我只知道像这样从字典转换为 JSON

NSMutableDictionary * location = [[NSMutableDictionary alloc]init];
    [location setValue:mobileCountryCode forKey:@"mobileCountryCode"];
    [location setValue:mobileNetworkCode forKey:@"mobileNetworkCode"];
    [location setValue:cellId forKey:@"cellId"];
    [location setValue:locationAreaCode forKey:@"locationAreaCode"]; 


 NSData *data = [NSJSONSerialization dataWithJSONObject:requestbodyInputDict options:NSUTF8StringEncoding error:nil];
                NSString* jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                NSLog(@"jsonString.....%@",jsonString);
                NSData *requestBody = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

所以请帮助我如何扭转这个过程如何处理这个。

【问题讨论】:

  • 您的问题到底是什么?什么不工作?
  • ` String jsondata = "{\n" + " \"homeMobileCountryCode\": \""+mmc+"\",\n" + " \"homeMobileNetworkCode\":\""+ mnc+ "\",\n" + " \"cellTowers\": [\n" + " { \n\"cellId\": \""+cid+"\",\"locationAreaCode\": \""+lac+ "\",\"mobileCountryCode\": \""+mmc+"\",\"mobileNetworkCode\": \""+mnc+"\"} " + " ],\n" + " \"wifiAccessPoints\": [\n" + " ]\n" + "}"; ` 这是 android 中用于 json 数据的代码我想在 ios 中编写此代码
  • 我想要这个的 json 字符串和 json 字典。 @Larme
  • 用你的原始字符串替换“用\”它会工作

标签: ios iphone json dictionary


【解决方案1】:

只需检查此代码。它会将您的 json 转换为 NSdictionary

   NSString *jsonString = @"{ \"homeMobileCountryCode\": 310, \"homeMobileNetworkCode\": 260, \"radioType\": \"gsm\", \"carrier\": \"T-Mobile\", \"cellTowers\": [ { \"cellId\": 39627456, \"locationAreaCode\": 40495, \"mobileCountryCode\": 310, \"mobileNetworkCode\": 260, \"age\": 0, \"signalStrength\": -95 } ], \"wifiAccessPoints\": [ { \"macAddress\": \"01:23:45:67:89:AB\", \"signalStrength\": 8, \"age\": 0, \"signalToNoiseRatio\": -65, \"channel\": 8 } ] }";

    NSError *jsonError;
    NSData *objectData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                         options:NSJSONReadingMutableContainers
                                                           error:&jsonError];

使用字符串替换方法将“替换为\”就可以了

【讨论】:

  • 需要任何帮助然后告诉我
  • 我也没有 json 字符串 ... :( @anil
  • 那么你有什么字典或 NSData
  • 我只有{ "homeMobileCountryCode": 310, "homeMobileNetworkCode": 260, "radioType": "gsm", "carrier": "T-Mobile", "cellTowers": [ { "cellId": 39627456, "locationAreaCode": 40495, "mobileCountryCode": 310, "mobileNetworkCode": 260, "age": 0, "signalStrength": -95 } ], "wifiAccessPoints": [ { "macAddress": "01:23:45:67:89:AB", "signalStrength": 8, "age": 0, "signalToNoiseRatio": -65, "channel": 8 } ] }
  • 我想在ios中写一个这样的字符串String jsondata = "{\n" + " \"homeMobileCountryCode\": \""+mmc+"\",\n" + " \"homeMobileNetworkCode\":\""+ mnc+"\",\n" + " \"cellTowers\": [\n" + " { \n\"cellId\": \""+cid+"\",\"locationAreaCode\": \""+lac+"\",\"mobileCountryCode\": \""+mmc+"\",\"mobileNetworkCode\": \""+mnc+"\"} " + " ],\n" + " \"wifiAccessPoints\": [\n" + " ]\n" + "}";
【解决方案2】:

您正在做的是直接访问“mobileCountryCode”、“mobileNetworkCode”等。它们是 json 数组“cellTowers”的元素,所以如果您想要这些值,您可以首先拥有一个“cellTowers”数组就这样……

【讨论】:

  • 我想找到手机信号塔的位置。用于搜索
  • 你可以得到一个蜂窝塔阵列,然后你可以得到位置,因为位置区号在 celltowers 阵列中,所以首先你必须得到一个蜂窝塔阵列/字典然后你可以得到地点
  • 我有单元格 id、lac、mnc 和 mcc,所以我只想通过搜索找到一个位置。为此,我创建了 4 个文本框。
  • 我想为这个{ "cellTowers": [ { "cellId": 21532831, "locationAreaCode": 2862, "mobileCountryCode": 214, "mobileNetworkCode": 7 } ] }创建一个字典
  • 用你的原始字符串替换“用\”它会起作用@anupgupta