iphone Objective-c :字符串到 NSData 转换以进行 json 反序列化

发布于 2024-11-15 00:38:33 字数 1293 浏览 5 评论 0原文

我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分分钟 2024-11-22 00:38:33

我认为它根本不是一个字符串......

更改为这个并测试......

NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:responseData error:&error];
NSDictionary *carsDictionary = [dictionary objectForKey:@"cars"];
NSArray *arrayOfCarDictionaries = [carsDictionary objectForKey:@"car"];

I think it's not a string at all....

change to this and test....

NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:responseData error:&error];
NSDictionary *carsDictionary = [dictionary objectForKey:@"cars"];
NSArray *arrayOfCarDictionaries = [carsDictionary objectForKey:@"car"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文