如何将印刷字典转换为 NSDictionary?
How to convert printed dictionary to NSDictionary?
当我 NSLog 字典输出是这样的:
{
accounts = (
{
"account_number" = 9000012;
"account_type" = "Saver Account";
ato = 0;
balance = "0.0";
},
{
"account_number" = 9000010;
"account_type" = "Primary Account";
ato = 0;
balance = "100000.0";
}
);
}
现在我需要在另一个程序中使用相同的字典(在我的测试中模拟)。
如何将这个值从控制台复制粘贴到 NSDictionary?
我试图将其设为 NSString,正如这个答案所解释的那样
NSString *jsonStr = @" { ({\"account_number\" = 9000012; \"account_type\" = \"Saver Account\"; ato = 0; balance = \"0.0\";});}";
NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
return dict;
Converting NSString to NSDictionary / JSON
但这是返回 null。
我再次将 = 替换为 : 并尝试
NSString *jsonStr = @" { ({\"account_number\" : 9000012; \"account_type\" : \"Saver Account\"; ato : 0; balance : \"0.0\";});}";
但还是不行?
已更新
我是这样转换的:
NSDictionary *account = @{ @"account_type":@"Account Type XXX",
@"account_number":@"Account Number 123" };
NSMutableDictionary *mainDict = [[NSMutableDictionary alloc] init];
[mainDict setValue:@[account] forKey:@"accounts"];
对于两个数组:
NSDictionary *account1 = @{ @"account_type":@"Account Type XXX",
@"account_number":@"Account Number 123" };
NSDictionary *account2 = @{ @"account_type":@"Account Type YYY",
@"account_number":@"Account Number 456" };
NSMutableDictionary *mainDict = [[NSMutableDictionary alloc] init];
[mainDict setValue:@[account1,account2] forKey:@"accounts"];
但是@Duncan C 声称这种方式不安全!
当您在控制台中打印字典时,它不会以 JSON
格式打印。因此,您必须将字典转换为 JSON
字符串,然后才能将其 return 转换回 NSDictionary
。您可以为此使用这些辅助方法(将对象转换为 JSON 字符串,将其转换回对象):
+ (NSData *)dataFromJSONObject:(id)object {
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error];
return data;
}
+ (NSString *)stringFromJSONObject:(id)object {
NSData *data = [self dataFromJSONObject:object];
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
+ (id)JSONObjectFromData:(NSData *)data {
NSError *error = nil;
id object = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
if (!object || error != nil) {
}
return object;
}
+ (id)JSONObjectFromString:(NSString *)string {
return [self JSONObjectFromData:[string dataUsingEncoding:NSUTF8StringEncoding]];
}
不要那样做。 NSLog 的输出没有任何特定格式,不保证 OS 版本之间保持一致。您将不得不编写一堆自定义代码,并且它很脆弱并且会因任何 OS 更改而中断。
您应该编写一个函数来获取您的字典并将其输出为漂亮 JSON。还要编写另一个函数,将 JSON 作为输入,returns 作为字典。然后从调试器调用这些函数。
编辑:
我创建了一个 Github 项目,它定义了一个 NSDictionary 类别 NSDictionary+JSON
DictionaryToJSON project on Github
如果您将 NSDictionary+JSON.h
和 NSDictionary+JSON.m
添加到您的项目中,并在任何使用它的文件中添加 #import
header,您可以在调试器:
e [someDictionary jsonString]
这将需要一些字典并将其转换为 JSON 字符串。它还会转义字符串中的任何引号,以便您可以 copy/paste 它,然后您可以转到您的其他程序(也包含该类别)并键入
e dict = [NSDictionary dictionaryFromJSONString: @"<the JSON string>"]
(将 "<the JSON string>"
替换为从上一个调试器命令中获得的实际 JSON 字符串。
请注意,为了能够从调试器控制台 copy/paste JSON 并将其转换为调试器命令,我必须将 JSON 数据中的任何引号转义为 \"
.
在您的应用程序包中添加一个 JSON 类型的文件并以编程方式读取它。
以下是您的操作方法:
(1) 打开 TextEdit
并创建新文档。
(2) 在该空文档中,PASTE Web 服务的响应。我使用 Postman
来获得响应。它采用 JSON
格式。您可以使用任何 JSON 字符串(在您的情况下来自其他项目..)。
(3) 单击 格式 > 制作纯文本
(4) 另存为:"yourFileName.json"
(5) 现在,只需将此 json 文件放入您的应用程序包中即可。
(6)阅读内容:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"yourFileName" ofType:@"json"];
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:filePath];
NSError *error = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSMutableArray *arrUsers = jsonDict[kData];
(7) 享受 :D
当我 NSLog 字典输出是这样的:
{
accounts = (
{
"account_number" = 9000012;
"account_type" = "Saver Account";
ato = 0;
balance = "0.0";
},
{
"account_number" = 9000010;
"account_type" = "Primary Account";
ato = 0;
balance = "100000.0";
}
);
}
现在我需要在另一个程序中使用相同的字典(在我的测试中模拟)。 如何将这个值从控制台复制粘贴到 NSDictionary?
我试图将其设为 NSString,正如这个答案所解释的那样
NSString *jsonStr = @" { ({\"account_number\" = 9000012; \"account_type\" = \"Saver Account\"; ato = 0; balance = \"0.0\";});}";
NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
return dict;
Converting NSString to NSDictionary / JSON
但这是返回 null。
我再次将 = 替换为 : 并尝试
NSString *jsonStr = @" { ({\"account_number\" : 9000012; \"account_type\" : \"Saver Account\"; ato : 0; balance : \"0.0\";});}";
但还是不行?
已更新
我是这样转换的:
NSDictionary *account = @{ @"account_type":@"Account Type XXX",
@"account_number":@"Account Number 123" };
NSMutableDictionary *mainDict = [[NSMutableDictionary alloc] init];
[mainDict setValue:@[account] forKey:@"accounts"];
对于两个数组:
NSDictionary *account1 = @{ @"account_type":@"Account Type XXX",
@"account_number":@"Account Number 123" };
NSDictionary *account2 = @{ @"account_type":@"Account Type YYY",
@"account_number":@"Account Number 456" };
NSMutableDictionary *mainDict = [[NSMutableDictionary alloc] init];
[mainDict setValue:@[account1,account2] forKey:@"accounts"];
但是@Duncan C 声称这种方式不安全!
当您在控制台中打印字典时,它不会以 JSON
格式打印。因此,您必须将字典转换为 JSON
字符串,然后才能将其 return 转换回 NSDictionary
。您可以为此使用这些辅助方法(将对象转换为 JSON 字符串,将其转换回对象):
+ (NSData *)dataFromJSONObject:(id)object {
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error];
return data;
}
+ (NSString *)stringFromJSONObject:(id)object {
NSData *data = [self dataFromJSONObject:object];
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
+ (id)JSONObjectFromData:(NSData *)data {
NSError *error = nil;
id object = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
if (!object || error != nil) {
}
return object;
}
+ (id)JSONObjectFromString:(NSString *)string {
return [self JSONObjectFromData:[string dataUsingEncoding:NSUTF8StringEncoding]];
}
不要那样做。 NSLog 的输出没有任何特定格式,不保证 OS 版本之间保持一致。您将不得不编写一堆自定义代码,并且它很脆弱并且会因任何 OS 更改而中断。
您应该编写一个函数来获取您的字典并将其输出为漂亮 JSON。还要编写另一个函数,将 JSON 作为输入,returns 作为字典。然后从调试器调用这些函数。
编辑:
我创建了一个 Github 项目,它定义了一个 NSDictionary 类别 NSDictionary+JSON
DictionaryToJSON project on Github
如果您将 NSDictionary+JSON.h
和 NSDictionary+JSON.m
添加到您的项目中,并在任何使用它的文件中添加 #import
header,您可以在调试器:
e [someDictionary jsonString]
这将需要一些字典并将其转换为 JSON 字符串。它还会转义字符串中的任何引号,以便您可以 copy/paste 它,然后您可以转到您的其他程序(也包含该类别)并键入
e dict = [NSDictionary dictionaryFromJSONString: @"<the JSON string>"]
(将 "<the JSON string>"
替换为从上一个调试器命令中获得的实际 JSON 字符串。
请注意,为了能够从调试器控制台 copy/paste JSON 并将其转换为调试器命令,我必须将 JSON 数据中的任何引号转义为 \"
.
在您的应用程序包中添加一个 JSON 类型的文件并以编程方式读取它。
以下是您的操作方法:
(1) 打开 TextEdit
并创建新文档。
(2) 在该空文档中,PASTE Web 服务的响应。我使用 Postman
来获得响应。它采用 JSON
格式。您可以使用任何 JSON 字符串(在您的情况下来自其他项目..)。
(3) 单击 格式 > 制作纯文本
(4) 另存为:"yourFileName.json"
(5) 现在,只需将此 json 文件放入您的应用程序包中即可。
(6)阅读内容:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"yourFileName" ofType:@"json"];
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:filePath];
NSError *error = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSMutableArray *arrUsers = jsonDict[kData];
(7) 享受 :D