如何将 JSON 数据添加到 NSArray [英] how to add JSON data to an NSArray

查看:55
本文介绍了如何将 JSON 数据添加到 NSArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下的 json 数据.

<预><代码>[{"id":"2","imagePath":"image002.jpg","enDesc":"Nice Image 2"},{"id":"1","imagePath":"image001.jpg","enDesc":"Nice Image 1"}]

我将其分配给名为 NSArray *news 的变量.

现在我有三个不同的数组,如下所示.

NSArray *idArray;NSArray *pathArray;NSArray *descArray;

我想将新闻数据分配给这些数组,以便最终得到如下结果.

NSArray *idArray = @["2","1"];NSArray *pathArray = @["image002.jpg","image001.jpg"];NSArray *descArray = @["Nice Image 2","Nice Image 1"];

知道如何完成这项工作吗?

<小时>

在以下答案的帮助下,这就是我所做的.

pathArray = [[NSArray alloc] initWithArray:[news valueForKey:@"imagePath"]];

出于某些原因,我不想使用 NSMutableArray.

解决方案

您应该使用 JSONKit 或 TouchJSON 将您的 JSON 数据转换为 Dictionary.比你可以这样做:

NSArray *idArray = [dictionary valueForKeyPath:@"id"];//KVO

I have json data as below.

[
    {"id":"2","imagePath":"image002.jpg","enDesc":"Nice Image 2"},
    {"id":"1","imagePath":"image001.jpg","enDesc":"Nice Image 1"}
]

I am assigning this to variable named NSArray *news.

Now I have three different array as below.

NSArray *idArray;
NSArray *pathArray;
NSArray *descArray;

I want to assign data of news to these arrays so that finally I should have as below.

NSArray *idArray = @["2","1"];
NSArray *pathArray = @["image002.jpg","image001.jpg"];
NSArray *descArray = @["Nice Image 2","Nice Image 1"];

Any idea how to get this done?


With the help of below answer this is what I did.

pathArray = [[NSArray alloc] initWithArray:[news valueForKey:@"imagePath"]];

I don't wanted to use NSMutableArray for some reasons.

解决方案

You should use JSONKit or TouchJSON to convert your JSON data to Dictionary. Than you may do this :

NSArray *idArray = [dictionary valueForKeyPath:@"id"]; // KVO

这篇关于如何将 JSON 数据添加到 NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆