gpt4 book ai didi

iphone - 使用 NSJSONSerializer iOS 发布 http JSON 请求

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:15:48 26 4
gpt4 key购买 nike

这是我的代码:

在我的.m

NSArray *keys = [NSArray arrayWithObjects:@"Training_Code", @"Training_Duration",@"Training_Startdate",@"Training_Enddate",@"Trainer_ID",@"Training_Location",@"Comments",@"Keyword",@"NumberofDays", nil];
NSArray *objects = [NSArray arrayWithObjects:@"Training_Code", @"Training_Duration",@"Training_Startdate",@"Training_Enddate",@"Trainer_ID",@"Training_Location",@"Comments",@"Keyword",@"NumberofDays", nil];
NSData *jsonData;
NSString *jsonString;
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

if([NSJSONSerialization isValidJSONObject:jsonDictionary])
{
jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil];
jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
}

// Be sure to properly escape your url string.
NSURL *url1 = [NSURL URLWithString:@"http://xx.xx.xx.xxx/DeployiOSCalender/service1.asmx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url1];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: jsonData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];

NSError *errorReturned = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];

if (errorReturned) {
// Handle error.
}
else
{
NSError *jsonParsingError = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&jsonParsingError];
}

这是返回给我的数据:

{
d = "[{\"Training_Code\":\"1234 \",\"Training_Duration\":\"2hrs \",\"Training_Startdate\":\"14/02/2013 15:00:00\",\"Training_Enddate\":\"14/02/2013 17:00:00\",\"Trainer_ID\":1,\"Training_Location\":\"B-Wing Training room-4\",\"Comments\":\"C# training\",\"Keyword\":\"C#1234\",\"NumberofDays\":1},{\"Training_Code\":\"4321 \",\"Training_Duration\":\"16 \",\"Training_Startdate\":\"17/02/2013 10:30:00\",\"Training_Enddate\":\"17/02/2013 17:30:00\",\"Trainer_ID\":2,\"Training_Location\":\"A-Wing Training Room-6\",\"Comments\":\"Objective-C\",\"Keyword\":\"Obj-C4321\",\"NumberofDays\":2}]";
}

格式不正确。我想做成这样:

[{"Training_Code":"1234      ","Training_Duration":"2hrs      ","Training_Startdate":"14/02/2013 15:00:00","Training_Enddate":"14/02/2013 17:00:00","Trainer_ID":1,"Training_Location":"B-Wing Training room-4","Comments":"C# training","Keyword":"C#1234","NumberofDays":1},{"Training_Code":"4321      ","Training_Duration":"16        ","Training_Startdate":"17/02/2013 10:30:00","Training_Enddate":"17/02/2013 17:30:00","Trainer_ID":2,"Training_Location":"A-Wing Training Room-6","Comments":"Objective-C","Keyword":"Obj-C4321","NumberofDays":2}

注意:我的网络服务正在返回正确的 json 格式。

我还需要做些什么来实现这一目标。请提出建议。

最佳答案

您说您的 Web 服务正在返回正确的 JSON,但显然不是。您应该与您的 Web 服务开发人员交谈,找出他们返回格式错误的 JSON 的原因。简而言之,您从 Web 服务返回的内容无法解析为 JSON——除非您进行一些字符串提取(呃)。

考虑您的响应数据,更好地格式化:

{

d = "
[
{
\"Training_Code\":\"1234 \",
\"Training_Duration\":\"2hrs \",
\"Training_Startdate\":\"14/02/2013 15:00:00\",
\"Training_Enddate\":\"14/02/2013 17:00:00\",
\"Trainer_ID\":1,
\"Training_Location\":\"B-Wing Training room-4\",
\"Comments\":\"C# training\",
\"Keyword\":\"C#1234\",
\"NumberofDays\":1
},
{
\"Training_Code\":\"4321 \",
\"Training_Duration\":\"16 \",
\"Training_Startdate\":\"17/02/2013 10:30:00\",
\"Training_Enddate\":\"17/02/2013 17:30:00\",
\"Trainer_ID\":2,
\"Training_Location\":\"A-Wing Training Room-6\",
\"Comments\":\"Objective-C\",
\"Keyword\":\"Obj-C4321\",
\"NumberofDays\":2
}
]
";

}

首先,将您的数据响应转换为字符串:

NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

然后你需要把这个从上面去掉:

{
d = "

从底部开始

    ";

}

然后您可以使用以下代码替换所有转义引号:

json = [json stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];

此时,您的字符串应该是可解析的 JSON,如下所示:

[
{
"Training_Code":"1234 ",
"Training_Duration":"2hrs ",
"Training_Startdate":"14/02/2013 15:00:00",
"Training_Enddate":"14/02/2013 17:00:00",
"Trainer_ID":1,
"Training_Location":"B-Wing Training room-4",
"Comments":"C# training",
"Keyword":"C#1234",
"NumberofDays":1
},
{
"Training_Code":"4321 ",
"Training_Duration":"16 ",
"Training_Startdate":"17/02/2013 10:30:00",
"Training_Enddate":"17/02/2013 17:30:00",
"Trainer_ID":2,
"Training_Location":"A-Wing Training Room-6",
"Comments":"Objective-C",
"Keyword":"Obj-C4321",
"NumberofDays":2
}
]

所以你可以这样做:

id object = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&error];

现在 object 应该将包含两条记录的数组保存为字典。

不过,如果是我,我会回到网络服务开发人员那里,要求他们修复发送给您的响应。您不必处理所有这些字符串提取废话。

关于iphone - 使用 NSJSONSerializer iOS 发布 http JSON 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14961239/

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