将 NSString 转换为 NSDictionary
Posted
技术标签:
【中文标题】将 NSString 转换为 NSDictionary【英文标题】:Convert NSString in to NSDictionary 【发布时间】:2018-02-19 06:00:55 【问题描述】:我正在使用 JSON POST 方法,我的代码是
[_urlReq setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[_urlReq setHTTPMethod:@"POST"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long) [postData length]];
[_urlReq setValue:postLength forHTTPHeaderField:@"Content-Length"];
[_urlReq setHTTPBody:postData];
_dataTask = [_urlSession dataTaskWithRequest:_urlReq completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
if (!(data == nil))
我正在将响应数据转换为字符串格式
NSString* myString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];// NSUTF8StringEncoding
NSLog(@"myString :%@", myString);
OutPut:
myString :"Response":"status":"SUCCESS","error_code":"0","message":"message","Array":["name" => "VVV","name" => "CHE","ac" => "5a8805a","email" =>"vvv","line1" =>"","line2" =>"","no" =>"","street" =>"","pin_code" =>"","state_id" =>"0","district_name" =>"0"]
//Convert string into Dic
NSData *data1 = [myString dataUsingEncoding: NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data1 options:0 error:nil];
NSLog(@"json : %@", json);
NSLog(@"Response : %@",[json objectForKey:@"Response"]);
//Second approach to convert string to dic
NSError *err = nil;
NSArray *array = [NSJSONSerialization JSONObjectWithData:[myString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&err];
NSLog(@"array :%@", array);
NSDictionary *dictionary = [array objectAtIndex:0];
NSLog(@"dictionary : %@", dictionary);
NSString *test = [dictionary objectForKey:@"Response"];
NSLog(@"Test is %@",test);
我不知道这里有什么问题......
【问题讨论】:
你检查错误err
了吗?
我从服务器收到这样的响应 myString :"Response":"status":"SUCCESS","error_code":"0","message":"message","数组":["name" => "VVV","name" => "CHE","ac" => "5a8805a","email" =>"vvv","line1" =>""," line2" =>"","no" =>"","street" =>"","pin_code" =>"","state_id" =>"0","district_name" =>"0"] 。但是当从 String 转换为 Dic 时没有得到任何错误
NSArray *array = [NSJSONSerialization JSONObjectWithData:[myString dataUsingEncoding:NSUTF8StringEncoding] 选项:NSJSONReadingMutableContainers 错误:&err]; NSLog(@"array :%@", array); ?? Y 转换 NSArray ??
您的 json 字符串无效。请提供一个有效的 json 字符串。
@McDonal_1 我已经这样做了 //Second approach to convert string to dic NSError *err = nil; NSArray *array = [NSJSONSerialization JSONObjectWithData:[myString dataUsingEncoding:NSUTF8StringEncoding] 选项:NSJSONReadingMutableContainers 错误:&err]; NSLog(@"array :%@", array);
【参考方案1】:
你的 JSON 格式是以字典开头的。
而不是,
NSArray *array = [NSJSONSerialization JSONObjectWithData:[myString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&err];
NSLog(@"array :%@", array);
替换它并尝试
NSDictionary *jSonDict = [NSJSONSerialization JSONObjectWithData:[myString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&err];
第二次尝试:
NSDictionary *jSonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
有关更多信息,如何验证 JSON,请使用此链接 enter link description here
您的 JSON:
【讨论】:
我得到 Null NSLog ("%@", jSonDict); 您可以直接将数据转换为字典。 @ McDonal_11 不,当我直接从数据转换为 dic 时,它会变为空。当我仅从数据转换为字符串时,我收到此响应 "Response":"status":"SUCCESS","error_code":"0","message":"message","Array":[ "name" => "VVV","name" => "CHE","ac" => "5a8805a","email" =>"vvv","line1" =>"","line2" =>" ","no" =>"","street" =>"","pin_code" =>"","state_id" =>"0","district_name" =>"0"] 为什么我不这样做不知道 在邮递员中,我收到这样的响应 "Response":"status":"SUCCESS","error_code":"0","message":"message","Array ":["name" => "VVV","name" => "CHE","ac" => "5a8805a","email" =>"vvv","line1" =>"","line2 " =>"","door_no" =>"","street" =>"","pin_code" =>"","state_id" =>"0","district_name" =>"0"] 你可以发截图吗?如果可能的话?【参考方案2】:用这个替换 JSON 响应字符串
"Response":"status":"SUCCESS","error_code":"0","message":"message","Array":["name" : "VVV","name" : "CHE","ac" : "5a8805a","email" : "vvv","line1" : "","line2" : "","no" : "","street" : "","pin_code" : "","state_id" : "0","district_name" : "0"]
【讨论】:
【参考方案3】:您的 json 字符串不正确。请检查我的演示 json 字符串。
试试这个
let myString :String = "\"Response\":\"status\":\"SUCCESS\",\"error_code\":\"0\",\"message\":\"message\",\"Array\":[\"name\" : \"VVV\",\"name\" : \"CHE\",\"ac\" : \"5a8805a\",\"email\" :\"vvv\",\"line1\" :\"\",\"line2\" :\"\",\"no\" :\"\",\"street\" :\"\",\"pin_code\" :\"\",\"state_id\" :\"0\",\"district_name\" :\"0\"]"
if let data = myString.data(using: .utf8)
do
let json = try JSONSerialization.jsonObject(with: data, options: .init(rawValue: 0));
print(json)
catch
输出
Response =
Array = (
ac = 5a8805a;
"district_name" = 0;
email = vvv;
line1 = "";
line2 = "";
name = VVV;
no = "";
"pin_code" = "";
"state_id" = 0;
street = "";
);
"error_code" = 0;
message = message;
status = SUCCESS;
;
【讨论】:
以上是关于将 NSString 转换为 NSDictionary的主要内容,如果未能解决你的问题,请参考以下文章
将 NSString 转换为具有日期格式的 NSString 块
将 'NSString *' 转换为 'CTStringRef *' 的不兼容类型