使用NSJsonSerialization在iOS7中通过发布方法使用JSON解析来获取数据

穆鲁格什坦克

我是iOS开发的新手。需要帮助来使用NSJSONSerialization解析此数据。数据来自发布方法。数据是这样的[{“ edition_id”:“ 1”,“ error”:false,“ long_name”:“ Rajkot”,“ message”:“ RESULT_OK”,“ short_name”:“ RJT”}]

我文件的代码是这样的

NSURL *theURL = [NSURL URLWithString:@"MYURL"];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];
[theRequest setURL:theURL];
[theRequest setHTTPMethod:@"POST"];
NSData *allCoursesData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:theRequest]];
NSError *error;
NSMutableDictionary *allCourses = [NSJSONSerialization JSONObjectWithData:allCoursesData options:kNilOptions error:&error];
马赫什(Mahesh Cheliya)
NSString * uid = @"2"; // variable value
NSString *post = [NSString stringWithFormat:@"uid=%@",uid]; // post variable
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://localhost/my/json_post/index.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSURLResponse *response;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
/* NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] length:[POSTReply length] encoding: NSASCIIStringEncoding];
NSLog(@"json Reply : %@", theReply);
*/

NSArray *arr = [NSJSONSerialization JSONObjectWithData:POSTReply options:0 error:nil];
NSLog(@"%@",arr);

NSLog(@"%@",[arr valueForKey:@"id"]); //this is table column name
NSLog(@"%@",[arr valueForKey:@"name"]);
NSLog(@"%@",[arr valueForKey:@"surname"]);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在iOS中使用NSJSONSerialization进行JSON解析

通过使用VBA在EXCEL中输入ISBN来解析Google Books JSON以获取图书信息

iOS使用NSJSONSerialization在POST请求中发送JSON数据

如何使用NSURLSESSION和NSJSONSerialization在ios目标C中发布数据?

通过使用 pyspark 将列转换为行来解析数据框中的 Json 字符串列表?

ios swift parse:从解析中获取数据并使用它们

无法获取componentDidMount()来通过this.setState使用获取的JSON数据更新空数组

在ios7中使用自定义警报视图的替代方法是什么?

如何使用swift调用iOS7中的CLLocation Manager委托方法

发布IOS7应用

使用http.PostForm来发布嵌套的json数据值?

使用NSBundle访问iOS7中的资产

如何使iOS7 App用于发布存折,请使用ibeacon(例如MLB)

使用 JSON 解析获取 OpenWeathermapApi 数据

我是否可以通过获取使用await调用的数据(而非异步)的方法来更改UI?

如何在不使用iOS7弃用方法的情况下获取AirPlay设备名称

iOS7中未发布NSData和UIImage

如何在Angular 7中使用http发布请求获取数据

通过使用 ACCOUNT_USERNAME_BLACKLIST (=[]) 并从 .txt 文件中获取数据来禁止某些用户

无法使用JSON从JQuery Ajax发布中获取PHP中的数据

如何使用占位符通过 API 发布 Json 元数据

如何使用VBScript通过HTTP API发布JSON数据?

通过AJAX使用JSON数据发布到php

无法在iOS7上使用scrollsToTop

iOS发布数据以获取JSON响应

Grails通过在对象的键中添加方括号[]来错误地解释发布的JSON数据

如何使用JSON数据获取API发布请求

将数据发布到PHP并使用SwiftyJSON获取JSON

使用 Android Volley 发布和获取 Json 数据