解析json时遇到问题
我是 iphone 和 json 世界的新手。我有这个 json 结构。把它放在这里 http://jsonviewer.stack.hu/ 就可以清楚地看到它。
{"@uri":"http://localhost:8080/RESTful/resources/prom/","promotion":[{"@uri":"http://localhost:8080/RESTful/resources/prom/1/","descrip":"description
here","keyid":"1","name":"The first
name bla bla
","url":"http://localhost/10.png"},{"@uri":"http://localhost:8080/RESTful/resources/promo/2/","descrip":"description
here","keyid":"2","name":"hello","url":"http://localhost/11.png"}]}
我想用 json-framework 解析它。我尝试了这个
NSDictionary *json = [myJSON JSONValue];
NSDictionary *promotionDic = [json objectForKey:@"promotion"];
NSLog(@" res %@ : ",[promotionDic objectAtIndex:0]);
但是如何获取例如索引 0 处的对象的名称?我想我应该将对象放入 NSArray 中?但我不知道如何:/并且我不知道对象的数量是可变的。请帮忙。
i'm new in the iphone and json world . i have this json structure . You can see it clearly by putting it here http://jsonviewer.stack.hu/ .
{"@uri":"http://localhost:8080/RESTful/resources/prom/","promotion":[{"@uri":"http://localhost:8080/RESTful/resources/prom/1/","descrip":"description
here","keyid":"1","name":"The first
name bla bla
","url":"http://localhost/10.png"},{"@uri":"http://localhost:8080/RESTful/resources/promo/2/","descrip":"description
here","keyid":"2","name":"hello","url":"http://localhost/11.png"}]}
i want to parse it with json-framework . I tried this
NSDictionary *json = [myJSON JSONValue];
NSDictionary *promotionDic = [json objectForKey:@"promotion"];
NSLog(@" res %@ : ",[promotionDic objectAtIndex:0]);
But then how to do to get for exemple , the name of the object at index 0 ? I think i should put object in an NSArray ? but i dont find how :/ and i dont the number of object is variable . Help please .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,加载 JSON 后您需要这一行,
它会告诉您您拥有什么。
其次,您不能在字典上调用 objectAtIndex: 。如果它有效,那是因为 PromotionDict 实际上是一个 NSArray。 (NSLog 会告诉你)。在 Objective-C 中,您可以分配给任何类型的指针,这会让新开发人员感到困惑,但它是语言的一部分,而且确实是一个功能。
使用 NSArray,您可以询问其中的内容数量、[myArray count] 等。您需要在 XCode 中命令双击以打开 NSDictionary 等的文档。
First off, You need this line after you load the JSON
That will tell you what you have.
Secondly, you can't call objectAtIndex: on a dictionary. If it works, its because promotionDict is really an NSArray. (The NSLog will tell you). In Objective - C you can assign to any kind of pointer, which is confusing to new developers, but is part of the language, and really is a feature.
With an NSArray you can ask for the number of things in it, [myArray count], etc. You need to command double click in XCode to open up the docs for NSDictionary, etc.
JSON 表示:
“
[
”意味着“促销”是一个数组,而不是字典。所以你的代码实际上应该是:
The JSON says:
That "
[
" there means that "promotion" is keyed to an array, not a dictionary.So really your code should be: