【问题标题】:How to use JSONValue with NSMutableArray如何将 JSONValue 与 NSMutableArray 一起使用
【发布时间】:2011-07-31 09:15:06
【问题描述】:

这是我的代码,但它不起作用

NSError *theError = nil;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.bbblllaaahhh.com"]];
NSURLResponse *theResponse =[[NSURLResponse alloc]init];    
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];      
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

self.city = [[NSMutableArray arrayWithArray:[string componentsSeparatedByString:@"\""]] JSONValue];

这里是 JSON 文本

    [
{
"kanji_name":"\u30ac\u30fc\u30c7\u30f3\u30d5\u30a3\u30fc\u30eb\u30ba\u3000\u3068\u306d\u308a\u516c\u5712BigBell"
}
]

它在self.city line中报告,我该怎么办??

是的!我完全修复了这是我的修复代码

NSError *theError = nil;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.blahblah.com"]];
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];   
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [string JSONValue];
NSArray *jsonArray = [NSArray arrayWithArray:(NSArray *)[jsonDict valueForKey:@"kanji_name"]];
NSMutableString *text = [[NSMutableString alloc] init];
[text appendFormat:@"%@",[jsonArray objectAtIndex:0]];
self.city = [NSMutableArray arrayWithObject:text];

【问题讨论】:

  • 编辑您的问题以显示您正在解析的 JSON 字符串的相关摘录(包括顶级对象)。
  • 感谢您对新手的帮助:)
  • 能否提供错误信息?
  • 这是我的代码帖子和代码注释中的错误。 -[__NSArrayM JSONValue]:由于未捕获的异常“NSInvalidArgumentException”,无法识别的选择器发送到实例 0x5854280 正在终止应用程序,原因:“-[__NSArrayM JSONValue]:无法识别的选择器发送到实例 0x5854280”

标签: iphone json ios4 sdk


【解决方案1】:

需要几个问题来帮助确定此问题的答案。

首先,你可以放置

NSLog(@"%@",data);

在第一行之后

NSLog(@"%@", string);

在第二行之后告诉我们它向控制台报告了什么值?这将有助于确定问题是否存在

1) 服务器永远不会返回任何数据或返回错误的数据,并且 2)如果数据正确地变成了一个字符串。如果其中任何一个操作失败,则可能会导致第 3 行出错。

接下来,你能报告第三行给出的错误吗?有很多可能的问题。实际上,字符串可能不是正确的 JSON 代码,并且 JSON 解析器正在崩溃。

第 3 行看起来有一个明显的问题。首先,您要根据“\”字符拆分字符串,在这种情况下,这似乎是一件不寻常的事情。但无论如何,操作顺序会是这样的:

@"a\b"

会变成

["a", "b"]

那么JSON解析器会尝试解析["a", "b"],这样肯定会报错。

至少,你会想做一些类似的事情,

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];      
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSArray or NSDictionary *parserResults = [string JSONValue];

// This will depend on what the actual JSON string returned from the server
// With an array, maybe something like
NSString *stringWithBackslash = [NSArray objectAtIndex:0];

// With a dictionary, maybe something like
NSString *stringWithBackslash = [NSDictionary objectForKey:@"backslashString"];

self.city = [NSMutableArray arrayWithArray:[stringWithBackslash componentsSeparatedByString:@"\""]];

self.city 是 NSMutableArray 吗?变量名使它听起来应该是一个字符串。在这种情况下,您实际上会想要做类似的事情

NSMutableArray components = [NSMutableArray arrayWithArray:[stringWithBackslash componentsSeparatedByString:@"\""]];

// if the city is the first element of the array
self.city = [components objectAtIndex:0];

您还需要检查以确保组件具有多个元素,因为这也可能导致错误,例如,如果服务器返回错误或没有互联网连接。

【讨论】:

  • NS(@"%@",data) 显示很多 16 位数字,例如 Data 和 NS(@"%@", string) 显示 [{"kanji_name":"\u30ac\u30fc\u30c7\u30f3\u30d5\u30a3\u30fc\u30eb\u30ba\u3000\u3068\u306d\u308a\u516c\u5712BigBell"}]
  • 好的,听起来代码正在正确地从服务器检索数据......所以问题可能出在第 3 行。
  • 看起来你的 JSON 是一个数组,里面有一个字典,所以你可以用 NSArray *topLevel = [string JSONValue]; 获取字符串本身NSDictionary *secondLevel = [topLevel objectAtIndex:0]; NSString *string = [secondLevel objectForKey:@"kanji_name"];现在,以 \u... 开头的所有内容实际上就是所谓的 Unicode 字符。用 componentsSeparatedByString:@"\" 解析字符串没有帮助,因为这些实际上是单个字符,尽管计算机使用多个字符向您显示它们。
  • 您要分离的字符串的哪一部分是“BigBell”?
猜你喜欢
  • 1970-01-01
  • 2013-01-12
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
  • 2023-03-13
  • 2014-09-25
相关资源
最近更新 更多