iPhone:yajl 返回的变量类型

发布于 2024-09-05 19:01:10 字数 1424 浏览 7 评论 0原文

我对 iphone 编程很陌生,我想做以下事情:

  1. 从 JSON REST Web 服务器获取数据
  2. 使用 YAJL 解析接收到的数据
  3. 使用 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:

  1. get data from a JSON REST web server
  2. parse the received data using YAJL
  3. 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 技术交流群。

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

发布评论

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

评论(1

酒浓于脸红 2024-09-12 19:01:10

它返回一个 NSDictionaryNSCFDictionary 是一个私有子类,对于本​​次讨论并不重要。所以看起来你会检索如下内容:

NSDictionary * responseDictionary = ...;
NSArray * dataArray = [responseDictionary objectForKey:@"data"];
for (NSDictionary * dataPair in dataArray) {
  NSLog(@"val: %@, date: %@", [dataPair objectForKey:@"val"], [dataPair objectForKey:@"date"]);
}

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:

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