gpt4 book ai didi

ios - 如何使用 NSJSONSerialization 将 NSData 转换为 NSDictionary?

转载 作者:行者123 更新时间:2023-12-01 18:48:00 35 4
gpt4 key购买 nike

嗨,我在使用 NSDataNSDictionary 转换为 NSJSONSerialization 时遇到问题?

我使用我的代码获取数据,但无法将其转换为 json。

这是我的代码...

ViewController.h 文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<NSURLConnectionDelegate>
{
NSString *massage;
NSURL *url;
NSMutableURLRequest *request;
NSURLConnection *connection;
NSMutableData *httpbody;
NSMutableData *webData;
NSData *responceData;
NSDictionary *responceJson;
}

ViewController.m 文件
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

NSString *mobile = @"123456789";

NSString *password = @"Welcome123";

NSString *deviceId = @"123sdfg15641ert321ret";


url = [NSURL URLWithString:@"http://203.109.87.34:8585/d-cab/web/app_dev.php/api/v1"];

massage = [NSString stringWithFormat:@"{\"action\":\"login\",\"data\":{\"contact\":\"%@\",\"password\":\"%@\",\"deviceId\":\"%@\"}}",mobile,password,deviceId];
//NSLog(@"Body = %@", massage);
httpbody = [ NSMutableData dataWithBytes: [ massage UTF8String ] length: [ massage length ] ];
request = [NSMutableURLRequest requestWithURL: url];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: httpbody];
[request setTimeoutInterval:10.0];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
//NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
//[connection start];
if (connection) {
webData = [[NSMutableData alloc]init];
}

}

#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[webData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[webData appendData:data];
//NSLog(@"WebData = %@",webData);
//NSLog(@"=================================");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSError* error = nil;
responceJson = [NSJSONSerialization JSONObjectWithData:webData options:kNilOptions error:&error];
NSLog(@"ResponceJSON = %@",responceJson);

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Show error message
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error In connection" message:@"We are facing some problem in connection. Please Check your Internet Connection." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

请检查我的代码并告诉我我必须在哪里进行哪些更改?

最佳答案

嘿,感谢所有关注我问题的人......但我在我的问题中发现了问题。
我正在尝试将参数发送为字符串格式而不是字典格式,并且未指定要请求的 header 。这是我更新的代码...

NSDictionary *paramData = [[NSDictionary alloc]initWithObjectsAndKeys:mobile,@"contact",password,@"password",deviceId,@"deviceId", nil];
//NSLog(@"paramData = %@",paramData);
jsonParam = [[NSDictionary alloc]initWithObjectsAndKeys:@"login",@"action",paramData,@"data", nil];
//NSLog(@"jsonParam = %@",jsonParam);

httpbody = [NSJSONSerialization dataWithJSONObject:jsonParam options:0 error:nil];

request = [NSMutableURLRequest requestWithURL: url];
request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [httpbody length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: httpbody];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
webData = [[NSMutableData alloc]init];
}

它对我有用....

关于ios - 如何使用 NSJSONSerialization 将 NSData 转换为 NSDictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33388425/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com