PHP返回的json,Obj-C解析的一个例子

PHP返回的json,Obj-C无法解析  


PHP代码如下:

<?php

$arr = array(   


'name' =>'abc',  


'nick' => 'bbc'


);


$json_string = json_encode($arr);   


echo $json_string; 

?>

只是简单的返回一串json,OC代码如下:


    NSString *name = _username.text;

    NSString *pass = _password.text;

    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8888/login.php"];

    NSLog(@"%@",url);    

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];

    [request setHTTPMethod:@"POST"];

    NSString *str = [NSString stringWithFormat:@"name=%@&pass=%@",name,pass];

    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

    [request setHTTPBody:data];

    NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

     NSError *error;

    NSDictionary *result = [NSJSerialization JSONObjectWithData:received options:NSJSONReadingAllowFragments error:&error];

    NSString *str1 = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];

    NSLog(@"%@",str1);

    NSLog(@"%@",error);


打印出的日志是这样:

2014-03-04 10:17:17.179 word[50534:70b] http://127.0.0.1:8888/login.php

2014-03-04 10:17:17.469 word[50534:70b] string{"name":"abc","nick":"bbc"}

2014-03-04 10:17:17.470 word[50534:70b] Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=xxxxxxxx {NSDebugDescription=Invalid value around character 0.}


有人遇到过吗?折腾了好几个小时,完全没有进展。


你的php返回内容应该是{"name":"abc","nick":"bbc"}, 而不是string{"name":"abc","nick":"bbc"}

由于多了一个string, 因此json解析失败了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解析JSON文件,可以使用json-c库,具体步骤如下: 1. 引入json-c库的头文件 ```c #include <json-c/json.h> ``` 2. 读取JSON文件内容 ```c FILE *fp = fopen("filename.json", "r"); if (fp == NULL) { printf("Failed to open file\n"); return -1; } char buffer[1024]; fread(buffer, 1, 1024, fp); fclose(fp); ``` 3. 解析JSON文件内容 ```c struct json_object *json_obj = json_tokener_parse(buffer); ``` 4. 获取JSON对象的值 ```c // 获取字符串类型的值 const char *str_value; json_object_object_get_ex(json_obj, "key", &str_value); printf("%s\n", str_value); // 获取整数类型的值 int int_value; json_object_object_get_ex(json_obj, "key", &int_value); printf("%d\n", int_value); // 获取数组类型的值 struct json_object *arr_obj; json_object_object_get_ex(json_obj, "key", &arr_obj); int arr_len = json_object_array_length(arr_obj); for (int i = 0; i < arr_len; i++) { struct json_object *item = json_object_array_get_idx(arr_obj, i); // 处理数组项 } ``` 完整的示例代码如下: ```c #include <stdio.h> #include <json-c/json.h> int main() { FILE *fp = fopen("example.json", "r"); if (fp == NULL) { printf("Failed to open file\n"); return -1; } char buffer[1024]; fread(buffer, 1, 1024, fp); fclose(fp); struct json_object *json_obj = json_tokener_parse(buffer); const char *str_value; json_object_object_get_ex(json_obj, "name", &str_value); printf("name: %s\n", str_value); int age_value; json_object_object_get_ex(json_obj, "age", &age_value); printf("age: %d\n", age_value); struct json_object *hobby_arr_obj; json_object_object_get_ex(json_obj, "hobbies", &hobby_arr_obj); int hobby_arr_len = json_object_array_length(hobby_arr_obj); printf("hobbies:\n"); for (int i = 0; i < hobby_arr_len; i++) { struct json_object *item = json_object_array_get_idx(hobby_arr_obj, i); printf("- %s\n", json_object_get_string(item)); } json_object_put(json_obj); return 0; } ``` 需要注意的是,使用完json对象后,需要调用`json_object_put`函数释放内存。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值