从Json Object Objective C获取值

时间:2015-11-28 10:20:27

标签: ios objective-c json nsdictionary

我正在使用目标c,这是我的问题..

(
        {
        error = 0;
        newsletter = (
                     {
                      date = "2015-11-23";
                      description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop p";
                      id = 42;
                      image = "mylink/uploads/14482883361619729430.png";
                      "post_count" = 2;
                      "posted_by" = admin;
                      title = Testing;
                    },
                    {
                     date = "2015-11-28";
                     description = "I am testing";
                     id = 48;
                     image = "mylink/ips/uploads/14486977841870344075.jpg";
                     "post_count" = 1;
                     "posted_by" = admin;
                     title = "Ips Informa";
                    }
                    );
       }
)

上面是我的json字符串,正在从asihttpRequest

中检索

现在我想在表格视图中显示简报eg.date,title,description

我刚刚获得了以下数组:

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData //1
                                                                   options:NSJSONReadingMutableContainers
                                                                     error:&err];

我使用以下代码获取输出:

NSLog(@"NewLetter Title: %@", json);

计数如下:

NSLog(@"Count is - %lu",[json count]);

计数是 - 1

我是目标C的新手,我试图解决,但没有找到获取时事通讯数据的方法?

4 个答案:

答案 0 :(得分:1)

每个JSON消息都有一个顶级对象。如果你看一下JSON:

(
    {

您可以看到()词典{的顶级}数组NSArray。有关详细信息,请参阅json.org

所以你已经使用你的代码走上了正确的轨道。您只需要访问数组中的每个字典,但JSON实际上可以是任何字典,因此您需要进行防御性编码并在访问之前检查它是NSArray* json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&err]; if ([json isKindOfClass:[NSArray class]]) { for (NSDictionary *dict in json) { NSNumber *error = dict[@"error"]; NSArray *newsletter = dict[@"newsletter"]; for (NSDictionary *entry in newsletter) { NSNumber *identity = entry[@"id"]; NSString *description = entry[@"description"]; // etc. } } } else if (err) { NSLog(@"Error in JSON: %@", [err localizedDescription]); } else { NSLog(@"Unsupported JSON received"); }

NSLog(@"Received JSON: %@", [NSString stringWithUTF8String:[responseData bytes]]);

我不知道为什么你得到count = 1,因为它与你发布的JSON不匹配;您应该记录实际的JSON以进行调试:

{{1}}

答案 1 :(得分:0)

json现在是$model->save,因此请使用常规访问者:

NSDictionary

答案 2 :(得分:0)

void quickSort(int *arr, int elements) { #define MAX_LEVELS 300 int piv, beg[MAX_LEVELS], end[MAX_LEVELS], i=0, L, R, swap ; beg[0]=0; end[0]=elements; while (i>=0) { L=beg[i]; R=end[i]-1; if (L<R) { piv=arr[L]; while (L<R) { while (arr[R]>=piv && L<R) R--; if (L<R) arr[L++]=arr[R]; while (arr[L]<=piv && L<R) L++; if (L<R) arr[R--]=arr[L]; } arr[L]=piv; beg[i+1]=L+1; end[i+1]=end[i]; end[i++]=L; if (end[i]-beg[i]>end[i-1]-beg[i-1]) { swap=beg[i]; beg[i]=beg[i-1]; beg[i-1]=swap; swap=end[i]; end[i]=end[i-1]; end[i-1]=swap; } } else { i--; } } } 对象包含一个json(由括号表示),其中包含一个项目,这就是计数为1的原因。

使用

获取项目的内容
NSArray

json[0] (由大括号表示)。

使用

获取密钥NSDictionary的值
newsletter

此值再次为json[0][@"newsletter"]

继续按数字订阅(如NSArray)检索数组的项目,按键订阅继续检索字典项目(如[0]

答案 3 :(得分:-1)

如果您是Objective-C的新手,那么您必须学习以下所有内容:

一。 NSArray和NSDictionary类。

两个。方法isKindOfClass告诉你对象是什么。

三。迭代数组,最好使用快速枚举。

四个。使用objectForKey或最好使用索引表示法访问字典的键。

当你知道所有这四个时,你可能会开始研究解析JSON。

顺便说一句。你不可能得到“时事通讯的日期”,因为可能有任意数量的新闻通讯,而你有的通讯有两个日期。您真的需要考虑您接收的数据以及您想要使用的数据。