无法从String获取JSON

时间:2021-12-19 19:02:13

I am getting the value of newDetails as a string.When I try the following code I get an exception as

我将newDetails的值作为字符串获取。当我尝试以下代码时,我得到一个例外

[__NSArrayI dataUsingEncoding:]: unrecognized selector sent to instance.

[__NSArrayI dataUsingEncoding:]:发送到实例的无法识别的选择器。

I have declared newDetail as an NSString. Also, the values of both newDetail in this and below code are same. Here's the code:

我已将newDetail声明为NSString。此外,此代码和代码下的newDetail值都相同。这是代码:

newDetail = [response valueForKey:@"newDetail"];
//newDetail prints as {"number":1,"nid":"1","pId":"3","name":"","me":"","day":"1"}
        NSError *error;
        NSData* data = [newDetail dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary* json = [NSJSONSerialization
                              JSONObjectWithData:data
                              options:kNilOptions
                              error:&error];

But when I try the below code, it runs perfectly:

但是,当我尝试下面的代码时,它运行完美:

newDetail = @"    {\"number\":1,\"nid\":\"1\",\"pId\":\"3\",\"name\":\"\",\"me\":\"\",\"day\":\"1\"}";       
        NSError *error;
        NSData* data = [newDetail dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary* json = [NSJSONSerialization
                              JSONObjectWithData:data
                              options:kNilOptions
                              error:&error];

Can anyone tell me why I am getting an exception?

谁能告诉我为什么我会得到例外?

1 个解决方案

#1


1  

It seems to me that the following code returns an NSArray:

在我看来,以下代码返回NSArray:

newDetail = [response valueForKey:@"newDetail"];

I suspect this because of the error message, which states that you try to call the method -dataUsingEncoding: on a NSArray object.

我怀疑这是因为错误消息,它指出您尝试在NSArray对象上调用方法-dataUsingEncoding :.

But ... you mention it prints like:

但是......你提到它的打印像:

{"number":1,"nid":"1","pId":"3","name":"","me":"","day":"1"}

Which would mean it's a NSDictionary (key/value pairs).

这意味着它是一个NSDictionary(键/值对)。

You could log the class to be sure like the following:

你可以记录类,以确保如下:

NSLog(@"%@", [newDetail class]);

Your version works because you hardcode a string. You should get the same error if you'd try to serialize a NSDictionary or perhaps even a NSArray into a JSON string, like this:

您的版本有效,因为您对字符串进行硬编码。如果您尝试将NSDictionary或甚至NSArray序列化为JSON字符串,您应该得到相同的错误,如下所示:

// This should result in the same error, since we serialize 
// an empty dictionary into a JSON string.
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:@{}
                                                     options:kNilOptions
                                                       error:&error];

#1


1  

It seems to me that the following code returns an NSArray:

在我看来,以下代码返回NSArray:

newDetail = [response valueForKey:@"newDetail"];

I suspect this because of the error message, which states that you try to call the method -dataUsingEncoding: on a NSArray object.

我怀疑这是因为错误消息,它指出您尝试在NSArray对象上调用方法-dataUsingEncoding :.

But ... you mention it prints like:

但是......你提到它的打印像:

{"number":1,"nid":"1","pId":"3","name":"","me":"","day":"1"}

Which would mean it's a NSDictionary (key/value pairs).

这意味着它是一个NSDictionary(键/值对)。

You could log the class to be sure like the following:

你可以记录类,以确保如下:

NSLog(@"%@", [newDetail class]);

Your version works because you hardcode a string. You should get the same error if you'd try to serialize a NSDictionary or perhaps even a NSArray into a JSON string, like this:

您的版本有效,因为您对字符串进行硬编码。如果您尝试将NSDictionary或甚至NSArray序列化为JSON字符串,您应该得到相同的错误,如下所示:

// This should result in the same error, since we serialize 
// an empty dictionary into a JSON string.
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:@{}
                                                     options:kNilOptions
                                                       error:&error];