解析json时遇到问题

发布于 2024-11-08 19:57:58 字数 892 浏览 4 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最终幸福 2024-11-15 19:57:58

首先,加载 JSON 后您需要这一行,

        NSLog(@" json %@ : ",[json description]); 

它会告诉您您拥有什么。

其次,您不能在字典上调用 objectAtIndex: 。如果它有效,那是因为 PromotionDict 实际上是一个 NSArray。 (NSLog 会告诉你)。在 Objective-C 中,您可以分配给任何类型的指针,这会让新开发人员感到困惑,但它是语言的一部分,而且确实是一个功能。

使用 NSArray,您可以询问其中的内容数量、[myArray count] 等。您需要在 XCode 中命令双击以打开 NSDictionary 等的文档。

First off, You need this line after you load the JSON

        NSLog(@" json %@ : ",[json description]); 

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.

暖树树初阳… 2024-11-15 19:57:58

JSON 表示:

..."promotion":[{"@u...

[”意味着“促销”是一个数组,而不是字典。

所以你的代码实际上应该是:

NSDictionary *json    = [myJSON JSONValue];
NSArray *promotions   = [json objectForKey:@"promotion"];
NSLog(@"res: %@",[promotions objectAtIndex:0]); 

The JSON says:

..."promotion":[{"@u...

That "[" there means that "promotion" is keyed to an array, not a dictionary.

So really your code should be:

NSDictionary *json    = [myJSON JSONValue];
NSArray *promotions   = [json objectForKey:@"promotion"];
NSLog(@"res: %@",[promotions objectAtIndex:0]); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文