gpt4 book ai didi

ios - 在ios中将json转换成字典

转载 作者:行者123 更新时间:2023-11-28 21:32:15 25 4
gpt4 key购买 nike

我正在尝试将此 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];

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

最佳答案

只需检查这段代码。它会将您的 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];

使用String replace方法将"替换为\"就可以了

关于ios - 在ios中将json转换成字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35429157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com