将 NSString 转换为 NSDictionary / JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18736250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting NSString to NSDictionary / JSON
提问by GuybrushThreepwood
I have the following data saved as an NSString:
我将以下数据另存为NSString:
{
Key = ID;
Value = {
Content = 268;
Type = Text;
};
},
{
Key = ContractTemplateId;
Value = {
Content = 65;
Type = Text;
};
},
I want to convert this data to an NSDictionarycontaining the key value pairs.
我想将此数据转换为NSDictionary包含键值对的数据。
I am trying first to convert the NSStringto a JSONobjects as follows :
我首先尝试将 转换NSString为JSON对象,如下所示:
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
However when I try :
但是,当我尝试:
NSString * test = [json objectForKey:@"ID"];
NSLog(@"TEST IS %@", test);
I receive the value as NULL.
我收到的值为NULL.
Can anyone suggest what is the problem ?
任何人都可以建议是什么问题?
回答by Janak Nirmal
I believe you are misinterpreting the JSON format for key values. You should store your string as
我相信您误解了键值的 JSON 格式。您应该将字符串存储为
NSString *jsonString = @"{\"ID\":{\"Content\":268,\"type\":\"text\"},\"ContractTemplateID\":{\"Content\":65,\"type\":\"text\"}}";
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
Now if you do following NSLog statement
现在,如果您执行以下 NSLog 语句
NSLog(@"%@",[json objectForKey:@"ID"]);
Result would be another NSDictionary.
结果将是另一个 NSDictionary。
{
Content = 268;
type = text;
}
Hope this helps to get clear understanding.
希望这有助于得到清晰的理解。
回答by MobileDev
I think you get the array from response so you have to assign response to array.
我认为您从响应中获取数组,因此您必须将响应分配给数组。
NSError *err = nil; NSArray *array = [NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&err]; NSDictionary *dictionary = [array objectAtIndex:0];
NSString *test = [dictionary objectForKey:@"ID"];
NSLog(@"Test is %@",test);
回答by Woodstock
Use this code where str is your JSON string:
使用此代码,其中 str 是您的 JSON 字符串:
NSError *err = nil;
NSArray *arr =
[NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:&err];
// access the dictionaries
NSMutableDictionary *dict = arr[0];
for (NSMutableDictionary *dictionary in arr) {
// do something using dictionary
}
回答by Josh O'Connor
Swift 3:
斯威夫特 3:
if let jsonString = styleDictionary as? String {
let objectData = jsonString.data(using: String.Encoding.utf8)
do {
let json = try JSONSerialization.jsonObject(with: objectData!, options: JSONSerialization.ReadingOptions.mutableContainers)
print(String(describing: json))
} catch {
// Handle error
print(error)
}
}
回答by krish2me
Use the following code to get the response object from the AFHTTPSessionManagerfailure block; then you can convert the generic type into the required data type:
使用以下代码从AFHTTPSessionManager失败块中获取响应对象;然后您可以将泛型类型转换为所需的数据类型:
id responseObject = [NSJSONSerialization JSONObjectWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] options:0 error:nil];

