iphone Objective-c :字符串到 NSData 转换以进行 json 反序列化
我从 Web 服务接收 json 到 NSMutableData 中。
使用 TouchJson 将其转换为 NSDictionary。
NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:responseData error:&error];
NSString *strData = [dictionary objectForKey:@"cars"];
然后我从该字典的键中检索一个字符串。
该字符串如下所示
{
b = "http://schemas.datacontract.org/";
car = (
{
"car_name" = "Honda Civic";
year = 2011;
"dealer" = "local honda dealer";
"bought on" = {
nil = 1;
};
"license_number" = 1234567;
status = ReadyToGo;
}
)};
本质上,针对“car”键可以有“n”条记录。
当我尝试使用
NSData *jsonData = [strData dataUsingEncoding:NSUTF8StringEncoding];
and
NSData *jsonData = [strData dataUsingEncoding:[NSString defaultCStringEncoding]];
将上述内容转换为 NSData 时,但我
[__NSCFDictionary dataUsingEncoding:]: unrecognized selector sent to instance 0x532bb70
尝试了一些其他可用的编码,但 xcode 仍然出现问题。
我如何找出正在使用的编码?
这是我第一次尝试在 Objective-C 中反序列化 json。
我在这里错过了什么/做错了什么?
谢谢
I receive json from a webservice into a NSMutableData.
That gets converted into a NSDictionary using TouchJson.
NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:responseData error:&error];
NSString *strData = [dictionary objectForKey:@"cars"];
I then retrieve a string from a key from that dictionary.
The string looks like below
{
b = "http://schemas.datacontract.org/";
car = (
{
"car_name" = "Honda Civic";
year = 2011;
"dealer" = "local honda dealer";
"bought on" = {
nil = 1;
};
"license_number" = 1234567;
status = ReadyToGo;
}
)};
Essentially there can be 'n' records against the 'car' key.
when I try to convert the above to NSData using
NSData *jsonData = [strData dataUsingEncoding:NSUTF8StringEncoding];
and also
NSData *jsonData = [strData dataUsingEncoding:[NSString defaultCStringEncoding]];
but I get
[__NSCFDictionary dataUsingEncoding:]: unrecognized selector sent to instance 0x532bb70
I have tried a few other encodings available and xcode still threw up.
How can I figure out the encoding being used?
This is my first attempt at deserealizing json in objective-c.
What am I missing/doing wrong here?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为它根本不是一个字符串......
更改为这个并测试......
I think it's not a string at all....
change to this and test....