iPhone:yajl 返回的变量类型
我对 iphone 编程很陌生,我想做以下事情:
- 从 JSON REST Web 服务器获取数据
- 使用 YAJL 解析接收到的数据
- 使用 core-plot 用这些数据绘制图表
所以,第 1 项很好,我使用ASIHttpRequest 按预期运行 第三个几乎没问题(我仍然需要学习如何调整核心情节)。
我遇到的问题是关于第二项。 我使用 YAJL 因为它似乎是更快的解析器,所以为什么不尝试一下:)
这是从服务器获取数据并解析它们的代码部分:
// Get server data
response_data = [request responseData];
// Parse JSON received
self.arrayFromData = [response_data yajl_JSON];
NSLog(@"Array from data: %@", self.arrayFromData);
事实上,解析工作得很好,NSLog 输出是类似于:
2010-06-14 17:56:35.375 TEST_APP[3733:207] 数据数组:
{
data = (
{
val = 1317;
date = "2010-06-10T15:50:01+02:00";
},
{
val = 1573;
date = "2010-06-10T16:20:01+02:00";
},
........
{
val = 840;
date = "2010-06-11T14:50:01+02:00";
},
{
val = 1265;
date = "2010-06-11T15:20:01+02:00";
}
);
from = "2010-06-10T15:50:01+02:00";
to = "2010-06-11T15:20:01+02:00";
max = "2590";
}
根据 yajl-objc 解释 http://github.com/gabriel/yajl-objc,解析返回一个NSArray... 问题是...我不知道如何从中获取所有值,因为对我来说它看起来更像是 NSDictionary 而不是 NSArray...
您能帮忙吗?
多谢, Luc
edit1:碰巧这个对象实际上是一个 NSCFDictionary (!),当我尝试 objectFromKey 方法(应该在字典上工作,不是吗?)时,我仍然无法从中获取值,它失败了。
I'm quite new to iphone programming and I want to do the following stuff:
- get data from a JSON REST web server
- parse the received data using YAJL
- Draw a graph with those data using core-plot
So, 1th item is fine, I use ASIHttpRequest which runs as espected
3rd is almost fine (I still have to learn how to tune core-plot).
The problem I have is regarding 2nd item.
I use YAJL as it seems to be the faster parser, so why not give it a try :)
Here is the part of code that gets the data from the server and parse them:
// Get server data
response_data = [request responseData];
// Parse JSON received
self.arrayFromData = [response_data yajl_JSON];
NSLog(@"Array from data: %@", self.arrayFromData);
The parsing works quite well in fact, the NSLog output is something like:
2010-06-14 17:56:35.375 TEST_APP[3733:207] Array from data :
{
data = (
{
val = 1317;
date = "2010-06-10T15:50:01+02:00";
},
{
val = 1573;
date = "2010-06-10T16:20:01+02:00";
},
........
{
val = 840;
date = "2010-06-11T14:50:01+02:00";
},
{
val = 1265;
date = "2010-06-11T15:20:01+02:00";
}
);
from = "2010-06-10T15:50:01+02:00";
to = "2010-06-11T15:20:01+02:00";
max = "2590";
}
According to th yajl-objc explanations http://github.com/gabriel/yajl-objc, the parsing returns a NSArray...
The thing is... I do not know how to get all the values from it as for me it looks more like a NSDictionary than a NSArray...
Could you please help ?
Thanks a lot,
Luc
edit1: it happens that this object is actually a NSCFDictionary (!), I am still not able to get value from it, when I try the objectFromKey method (that should work on a Dictionary, no ?) it fails.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它返回一个
NSDictionary
。NSCFDictionary
是一个私有子类,对于本次讨论并不重要。所以看起来你会检索如下内容:It's returning an
NSDictionary
.NSCFDictionary
is a private subclass and is immaterial to this discussion. So it looks like you'd retrieve stuff like: