在iPhone中使用Brautaset Json框架时出错

时间:2021-09-26 01:43:15

I'm trying to make a http request. The code looks like this:

我正在尝试发出http请求。代码如下所示:

NSString *urlString = [NSString stringWithString:@"http://www.foo.se/bartojson.php?venue=47497"]; 


NSLog(@"retain %d urlString %@", [urlString retainCount], urlString );
    NSURL *url = [NSURL URLWithString:urlString];
    [urlString release];

    NSString *jsonString = [NSString stringWithContentsOfURL:url];
    NSDictionary *httpResult = [jsonString JSONValue];

When calling the page in a browser the result looks like this:

在浏览器中调用页面时,结果如下所示:

{"name": "test" ,"description": "This is a test." ,"reviews": [{"grade": "5", "description": "Nice"},{"grade": "3", "description": "Very nice!"}]}

{“name”:“test”,“description”:“这是一个测试。” ,“评论”:[{“等级”:“5”,“描述”:“不错”},{“等级”:“3”,“描述”:“非常好!”}}}

But when calling it from the code i get this:

但是从代码中调用它时我得到了这个:

Error Domain=org.brautaset.JSON.ErrorDomain Code=3 UserInfo=0x582640 "Object value expected for key: reviews"

Error Domain = org.brautaset.JSON.ErrorDomain Code = 3 UserInfo = 0x582640“密钥期望的对象值:评论”

There seems to be some problem with the reviews pointing to a new dictionary. Could you please help me understand this error message.

对于指向新词典的评论似乎存在一些问题。你能帮我理解这个错误信息。

2 个解决方案

#1


6  

Well I found the reason for my error. Stupid windows linebreaks within the result made the JSONValue call fail. Removed all \r and viola everything works like a charm!

好吧,我找到了我的错误的原因。结果中的愚蠢窗口换行使JSONValue调用失败。删除所有\ r \ n和中提琴一切都像魅力!

#2


2  

Dunno if it was a typo, but your example is not valid JSON, because of the comma following "Nice". JSON does not allow trailing commas in arrays or dictionaries ("Objects"). JSON.framework would certainly kick up a fuzz about that.

Dunno,如果它是一个拼写错误,但你的例子是无效的JSON,因为逗号跟随“Nice”。 JSON不允许在数组或词典中使用尾随逗号(“对象”)。 JSON.framework肯定会对此大肆宣传。

By the way, if you're using the current stable version of the framework it will always print the full stack of the failure to the Console log, if you're using the convenience methods. Otherwise, you can ask the parser object for a full stack trace and print it yourself.

顺便说一句,如果您使用的是当前稳定版本的框架,如果您使用的是方便方法,它将始终将完整的故障堆栈打印到Console日志中。否则,您可以向解析器对象请求完整的堆栈跟踪并自行打印。

#1


6  

Well I found the reason for my error. Stupid windows linebreaks within the result made the JSONValue call fail. Removed all \r and viola everything works like a charm!

好吧,我找到了我的错误的原因。结果中的愚蠢窗口换行使JSONValue调用失败。删除所有\ r \ n和中提琴一切都像魅力!

#2


2  

Dunno if it was a typo, but your example is not valid JSON, because of the comma following "Nice". JSON does not allow trailing commas in arrays or dictionaries ("Objects"). JSON.framework would certainly kick up a fuzz about that.

Dunno,如果它是一个拼写错误,但你的例子是无效的JSON,因为逗号跟随“Nice”。 JSON不允许在数组或词典中使用尾随逗号(“对象”)。 JSON.framework肯定会对此大肆宣传。

By the way, if you're using the current stable version of the framework it will always print the full stack of the failure to the Console log, if you're using the convenience methods. Otherwise, you can ask the parser object for a full stack trace and print it yourself.

顺便说一句,如果您使用的是当前稳定版本的框架,如果您使用的是方便方法,它将始终将完整的故障堆栈打印到Console日志中。否则,您可以向解析器对象请求完整的堆栈跟踪并自行打印。