将 json 数据解析到 iphone [英] parsing json feeds into iphone

查看:39
本文介绍了将 json 数据解析到 iphone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将公共 json flickr 提要解析到 iphone.我遇到错误..

-JSONValue 失败.错误跟踪是:("错误域=org.brautaset.JSON.ErrorDomain Code=3 \"无法识别的前导字符\" UserInfo=0x4b43100 {NSLocalizedDescription=无法识别的前导字符}").....

以下是 json 提要:

如果您使用来自上述 http://jsonlint.com 中的 URL 的响应,它会告诉您它是有效的 JSON.

nojsoncallback 记录在 http://www.flickr.com/services/api/response.json.html


如何阅读该回复的快速示例.JSON 字符串的顶层元素是一个对象,通常由 JSON 解析器映射到字典:

//从 Flickr API 获取 JSON 字符串NSString *jsonString = ...;//将 JSON 字符串解析为字典//(在本例中,通过 SBJSON)NSDictionary *responseObject = [jsonString JSONValue];//字典有一个条目叫做items",它是一个数组NSArray *items = [responseObject objectForKey:@"items"];//迭代项目.每个项目都是一个对象,因此是一个字典for (NSDictionary *item in items) {//每个条目字典都有一个名为author_id"的条目,它是一个字符串NSString *authorId = [item objectForKey:@"author_id"];//记录作者idNSLog(@"author_id = %@", authorId);}

i was trying to parse public json flickr feed into iphone.i m getting an error..

-JSONValue failed. Error trace is: (
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Unrecognised leading character\" UserInfo=0x4b43100 {NSLocalizedDescription=Unrecognised leading character}"
).....

below is the json feed:

http://api.flickr.com/services/feeds/photos_public.gne?tags=hackdayindia&lang=en-us&format=json.....

i d be so greatful if you guys could help me out

解决方案

Your response data is not valid JSON because of the jsonFlickrFeed() callback returned by the Flickr API. In order to obtain valid JSON, add nojsoncallback=1 to your query. Using your example, the corresponding URL is:

http://api.flickr.com/services/feeds/photos_public.gne?tags=hackdayindia&lang=en-us&format=json&nojsoncallback=1

If you use the response from the URL above in http://jsonlint.com, it’ll tell you it’s valid JSON.

nojsoncallback is documented in http://www.flickr.com/services/api/response.json.html


A quick example of how to read that response. The top level element of the JSON string is an object, which is usually mapped to a dictionary by JSON parsers:

// Obtain the JSON string from Flickr API
NSString *jsonString = …;

// Parse the JSON string into a dictionary
// (in this example, via SBJSON)
NSDictionary *responseObject = [jsonString JSONValue];

// The dictionary has an entry called "items", which is an array
NSArray *items = [responseObject objectForKey:@"items"];

// Iterate over the items. Each item is an object, hence a dictionary
for (NSDictionary *item in items) {
    // Each item dictionary has an entry called "author_id", which is a string
    NSString *authorId = [item objectForKey:@"author_id"];

    // Log the author id
    NSLog(@"author_id = %@", authorId);
}

这篇关于将 json 数据解析到 iphone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆