首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ios数组到字典或数组

ios数组到字典或数组
EN

Stack Overflow用户
提问于 2011-08-31 20:08:49
回答 3查看 6.6K关注 0票数 1

我收到了对我的应用程序Twitter web服务的JSON响应,是一个字符串,但例如索引0处的对象是:

代码语言:javascript
复制
es array en:0, tiene {
contributors = "<null>";
coordinates = "<null>";
"created_at" = "Thu Aug 04 23:26:05 +0000 2011";
favorited = 0;
geo = "<null>";
id = 99259843982016513;
"id_str" = 99259843982016513;
"in_reply_to_screen_name" = "<null>";
"in_reply_to_status_id" = "<null>";
"in_reply_to_status_id_str" = "<null>";
"in_reply_to_user_id" = "<null>";
"in_reply_to_user_id_str" = "<null>";
place = "<null>";
"possibly_sensitive" = 0;
"retweet_count" = 0;
retweeted = 0;
source = "<a href=\"http://twitter.com/tweetbutton\" rel=\"nofollow\">Tweet Button</a>";
text = "Stack Exchange Q&A site proposal: Freelance Workers http://t.co/yaW1RHp";
truncated = 0;
user =     {
    "contributors_enabled" = 0;
    "created_at" = "Mon Jul 13 19:39:31 +0000 2009";
    "default_profile" = 0;
    "default_profile_image" = 0;
    description = "My goal is to enable the brain computer interfaces to use the possibilities of mobile platforms for robotics and physical computing";
    "favourites_count" = 0;
    "follow_request_sent" = "<null>";
    "followers_count" = 92; ...

因此,对于每个twitt,我的数组有17个对象,那么我如何将这些对象分解为更多的数组或字典?

我特别想要文本密钥

代码语言:javascript
复制
text = "Apple vs Samsung tablets [haha and Samsung is an Apple hardware provider!!]\nhttp://t.co/rvv43Hy";

非常感谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-31 20:17:54

可能已经有为此构建的解析器,但如果没有,我认为您会发现以下方法很有用。

代码语言:javascript
复制
NSArray *strings = [input componentsSeparatedByString:@";"];

它返回一个字符串数组,(在本例中) ";“作为分隔符。

{投稿人= "",坐标= "“,...}

您可以进一步将它们分开:

代码语言:javascript
复制
NSDictionary *dict = [NSDictionary dictionary];
for (NSString *s in strings)
{
    NSArray *keyValue = [s componentsSeparatedByString:@"="];
    NSString *key = [keyValue objectAtIndex:0];
    NSString *value = [keyValue objectAtIndex:1];
    [dict setValue:value forKey:key];
}

在响应的开始似乎确实有一些额外的数据,你可能必须先去掉它。

票数 3
EN

Stack Overflow用户

发布于 2011-08-31 20:13:34

例如,从外部字典中提取"user“对象并将其赋给一个NSDictionary变量。然后,您可以从第二个字典中提取,比如说,"default_profile“。

人们可能会编写一个简单的“路径导航器”工具,它可以通过“路径表示法”访问单个实体,而不必显式地提取组件片段,但我不知道有什么“封装的”工具。

票数 2
EN

Stack Overflow用户

发布于 2011-08-31 21:01:22

以防其他人正在寻找它,我最终使用了

Stig Brautaset的开源JSON框架。以及它的解析器,并遵循rom here的一些说明。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7256715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档