ios利用josn解析(转载)

json不必多说了,在手机开发中,客户端和服务器端开发经常使用的数据交互模式。Iphone中利用json传递的数据,可以非常容易的展示到各个容器中。下面是一个最简单的例子。效果如图:

201008161650.jpg

上面用到了json传递的数据,有关json部分,iphone sdk虽然没有支持,但是第三方已经写好了。

json 参考:http://code.google.com/p/json-framework/

下面是具体的代码实现:

数据加载:

#import “MyDataSource.h”

#import “JSON.h”

@implementation MyDataSource

+ (NSDictionary *)fetchLibraryInformation

{

NSString *urlString = [NSString stringWithFormat:@"http://wangjun.easymorse.com/wp-content/video/hello.jison"];

NSURL *url = [NSURL URLWithString:urlString];

NSLog(@”fetching library data”);

return [self fetchJSONValueForURL:url];

}

+ (id)fetchJSONValueForURL:(NSURL *)url

{

NSString *jsonString = [[NSString alloc] initWithContentsOfURL:url

encoding:NSUTF8StringEncoding error:nil];

id jsonValue = [jsonString JSONValue];

[jsonString release];

return jsonValue;

}

@end

table数据展示:

#import “JSONTableTestViewController.h”

#import “MyDataSource.h”

@implementation JSONTableTestViewController

@synthesize myData;

- (void)viewDidLoad {

NSLog(@”加载数据“);

myData = [[MyDataSource fetchLibraryInformation] retain];

}

- (void)didReceiveMemoryWarning {

// Releases the view if it doesn’t have a superview.

[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren’t in use.

}

- (void)viewDidUnload {

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return [myData count]; //有多少个section,也就是“几家”

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [[myData valueForKey:[[myData allKeys] objectAtIndex:section]] count];

//这里我们需要告诉UITableViewController每个section里面有几个,也就是“一家里面有几口人”

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @”Cell”;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:CellIdentifier] autorelease];

}

//上面的东西都是重复白给的,平时没事不用想为什么,照抄就可以了

cell.textLabel.text = [[myData valueForKey:[[myData allKeys]objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

//这句看上去复杂,但是其实不过是在特定section里面找到对应的array,

//然后在array中找到indexPath.row所在的内容

return cell;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

return [[myData allKeys] objectAtIndex:section];

//这里设置对应section的名字,很简单allKey返回所有的键值为一个array,也就是“张家”,“李家”

//然后用objectAtIndex: 来找出究竟是哪一个就可以了!

}

- (void)dealloc {

[myData release];

[super dealloc];

}

@end

源代码见:http://easymorse.googlecode.com/svn/trunk/JSONTableTest/

### 如何进行 JSON 解析开发应用程序时,JSON 是一种常用的数据交换格式。对于不同平台和编程语言,存在多种方式来解析 JSON 数据。 #### Android 中的 JSON 解析 为了在 Android 应用程序中解析 JSON 并将其绑定到 ListView,可以采用以下方法[^1]: ```java // 假设已经获取到了 JSON 字符串 jsonStr try { JSONArray jsonArray = new JSONArray(jsonStr); ArrayList<HashMap<String, String>> list = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); HashMap<String, String> map = new HashMap<>(); // 将 JSON 对象中的键值对放入 Map 中 Iterator<String> iterator = jsonObject.keys(); while(iterator.hasNext()){ String key = iterator.next(); try{ Object value = jsonObject.get(key); map.put(key, value.toString()); } catch(JSONException e){ // 处理异常情况 } } list.add(map); } } catch (JSONException e) { e.printStackTrace(); } ``` 这段代码展示了如何遍历 JSON 数组并提取其中的对象属性,最终将这些数据存储在一个 `ArrayList` 的哈希表集合里以便后续用于填充 ListView 控件。 #### C/C++ 使用 cJSON 进行 JSON 解析 当涉及到嵌入式系统或者需要高效处理 JSON 文件的情况下,在 C 或者 C++ 程序里面使用 cJSON 库是一个不错的选择[^2]: ```c #include "cjson/cJSON.h" void parse_json(const char *json_string) { cJSON *root = cJSON_Parse(json_string); // 解析整个 JSON 文本 if (!root) { printf("Error before: [%s]\n", cJSON_GetErrorPtr()); return; } cJSON *data_obj = cJSON_GetObjectItemCaseSensitive(root, "data"); int sign_count = data_obj->valueint; cJSON *sign_list_array = cJSON_GetObjectItemCaseSensitive(root, "signList"); cJSON *item; cJSON_ArrayForEach(item, sign_list_array) { cJSON *finger_print_obj = cJSON_GetObjectItemCaseSensitive(item, "fingerPrint"); const char* fp_pic_base64 = finger_print_obj->valuestring; cJSON *sign_name_obj = cJSON_GetObjectItemCaseSensitive(item, "signName"); cJSON *sign_pic_base64_item = cJSON_GetObjectItemCaseSensitive(sign_name_obj, "signPicBase64"); cJSON *sign_xml_item = cJSON_GetObjectItemCaseSensitive(sign_name_obj, "signXML"); double x_pos = sign_name_obj["x"]->valuedouble; double y_pos = sign_name_obj["y"]->valuedouble; // 执行进一步操作... } cJSON_Delete(root); } ``` 此函数接收一个标准的 JSON 字符串作为输入参数,并演示了怎样访问特定字段以及迭代数组成员。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值