如何获取名称和名称的值来自 nsdictionary 对象的 url?

发布于 2024-09-12 05:56:38 字数 2106 浏览 2 评论 0原文

我正在使用 touch JSON,这对我来说效果很好。我能够获取一个数组,将其放入字典中,通过 touchJSON 序列化它并通过 http 发送出去。

现在,在返回端,我收到了数据,并将其放入字典中(我使用 Twitter 中的 trends.json 作为 JSON 示例)。

如果我尝试从字典对象中获取趋势值,我会得到以下结果:

2010-08-02 00:23:31.069 rateMyTaxi[30610:207] ANSWER: (
  {
    name = "Fried Chicken Flu";
    url = "http://search.twitter.com/search?q=Fried+Chicken+Flu";
  },
  {
    name = "Lisa Simpson";
    url = "http://search.twitter.com/search?q=Lisa+Simpson";
  },
  {
    name = "#breakuplines";
    url = "http://search.twitter.com/search?q=%23breakuplines";
  },
  {
    name = "#thingsuglypeopledo";
    url = "http://search.twitter.com/search?q=%23thingsuglypeopledo";
  },
  {
    name = "Inception";
    url = "http://search.twitter.com/search?q=Inception";
  },
  {
    name = "#sharkweek";
    url = "http://search.twitter.com/search?q=%23sharkweek";
  },
  {
    name = "JailbreakMe";
    url = "http://search.twitter.com/search?q=JailbreakMe";
  },
  {
    name = "Kourtney";
    url = "http://search.twitter.com/search?q=Kourtney";
  },
  {
    name = "Shark";
    url = "http://search.twitter.com/search?q=Shark";
  },
  {
    name = "Boondocks";
    url = "http://search.twitter.com/search?q=Boondocks";
  }
)

如果我尝试获取名称或 URL 的值,我不会得到任何令人沮丧的结果。这就是我需要的数据。您可以看出它是字典格式,因为它已格式化并且可以正确读取趋势。我很确定我错过了一些东西,所以请让我知道该遵循哪个方向。

这是代码:

// this is all touch JSON magic. responseString has the full contents of trends.json

 NSString *response = [request responseString];
 NSLog(@"response value is:%@",response);

 NSString *jsonString = response;
 NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
 NSError *error = nil;
 NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
//end of touchJSON.  It is in a dictionary now.

 NSLog(@"dictionary:%@, error %@", dictionary, error); //http://cl.ly/adb6c6a974c3e70fb51c

 NSString *twitterTrends = (NSString *) [dictionary objectForKey:@"trends"];
 NSLog(@"ANSWER:%@",twitterTrends); //http://cl.ly/fe270fe7f05a0ea8d478

I am using touch JSON which worked out pretty well for me. I was able to take an array, put it in a dictionary, serialize it via touchJSON and send it out via http.

Now on the return end, I received the data, and put it into a dictionary (I'm using trends.json from twitter as an example JSON).

If I try to get the value for trends from the dictionary object, I get this:

2010-08-02 00:23:31.069 rateMyTaxi[30610:207] ANSWER: (
  {
    name = "Fried Chicken Flu";
    url = "http://search.twitter.com/search?q=Fried+Chicken+Flu";
  },
  {
    name = "Lisa Simpson";
    url = "http://search.twitter.com/search?q=Lisa+Simpson";
  },
  {
    name = "#breakuplines";
    url = "http://search.twitter.com/search?q=%23breakuplines";
  },
  {
    name = "#thingsuglypeopledo";
    url = "http://search.twitter.com/search?q=%23thingsuglypeopledo";
  },
  {
    name = "Inception";
    url = "http://search.twitter.com/search?q=Inception";
  },
  {
    name = "#sharkweek";
    url = "http://search.twitter.com/search?q=%23sharkweek";
  },
  {
    name = "JailbreakMe";
    url = "http://search.twitter.com/search?q=JailbreakMe";
  },
  {
    name = "Kourtney";
    url = "http://search.twitter.com/search?q=Kourtney";
  },
  {
    name = "Shark";
    url = "http://search.twitter.com/search?q=Shark";
  },
  {
    name = "Boondocks";
    url = "http://search.twitter.com/search?q=Boondocks";
  }
)

If I try to get the value for name or URL, I get nothing which is frustrating. That is the data I will need. You can tell it is in a dictionary format because it is formatted and it is reading properly sort of for trends. I'm pretty sure I'm missing something, so please let me know which direction to follow.

Here is the code:

// this is all touch JSON magic. responseString has the full contents of trends.json

 NSString *response = [request responseString];
 NSLog(@"response value is:%@",response);

 NSString *jsonString = response;
 NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
 NSError *error = nil;
 NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
//end of touchJSON.  It is in a dictionary now.

 NSLog(@"dictionary:%@, error %@", dictionary, error); //http://cl.ly/adb6c6a974c3e70fb51c

 NSString *twitterTrends = (NSString *) [dictionary objectForKey:@"trends"];
 NSLog(@"ANSWER:%@",twitterTrends); //http://cl.ly/fe270fe7f05a0ea8d478

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

酒与心事 2024-09-19 05:56:38

当您使用格式字符串 %@ 打印对象时,您实际上将获得发送到对象的描述消息的输出。对于 NSDictionary,此输出看起来与未解析的 JSON 非常相似,但它不是字符串,而是 NSDictionaries、NSArrays、NSStrings、NSNumbers 和 NSDates 的对象图。

所以希望你在 twitterTrends 中有一个 NSDictionaries 的 NSArray。要获取零件,只需枚举数组即可。

for (NSDictionary* trend in twitterTrends) {
    NSString* url = [trend objectForKey:@"url"];
    NSString* name = [trend objectForKey:@"name"];
}

或者您可以通过索引访问趋势:

[[twitterTrends objectAtIndex:5] objectForKey:@"url"];

When you use print an object using a formatstring %@ you will infact get the output of the description message send to the object. For a NSDictionary this output looks very similar to unparsed JSON, but it is not a string, it's an object graph of NSDictionaries, NSArrays, NSStrings, NSNumbers and NSDates.

So want you have in twitterTrends an NSArray of NSDictionaries. To get the parts just enumerate the Array.

for (NSDictionary* trend in twitterTrends) {
    NSString* url = [trend objectForKey:@"url"];
    NSString* name = [trend objectForKey:@"name"];
}

Or you may access a trend by its index:

[[twitterTrends objectAtIndex:5] objectForKey:@"url"];
痴情 2024-09-19 05:56:38

您只能提取字典数组。 (响应在字典内的数组内有一个字典。)

您需要从数组中提取每个字典,然后查找该字典中的名称和 url 条目。

像这样的东西应该打印第一个条目:(

NSArray *twitterTrends = [dictionary objectForKey:@"trends"];
NSDictionary *entry1 = [twitterTrends objectAtIndex:0];
NSLog(@"entry1: %@, %@", [entry1 objectForKey:@"name"], [entry1 objectForKey:@"url"]);

代码尚未经过测试,因此可能无法直接编译!)

You've only got as far as extracting the array of dictionaries. (The response has a dictionary inside an array inside a dictionary.)

You need to extract each dictionary from the array, then lookup the name and url entries in that dictionary.

Something like this should print the first entry:

NSArray *twitterTrends = [dictionary objectForKey:@"trends"];
NSDictionary *entry1 = [twitterTrends objectAtIndex:0];
NSLog(@"entry1: %@, %@", [entry1 objectForKey:@"name"], [entry1 objectForKey:@"url"]);

(Code has not been tested so may not compile straight off!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文