2

这是我的代码:

在我的.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 格式。

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

4

2 回答 2

2

您说您的 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应该将包含您的两条记录的数组保存为字典。

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

于 2013-02-20T17:55:29.970 回答
0

Trainer_ID 和 NumberofDays 是原始的。您不能按原样将 then 添加到 NSArray 中。您应该将它们包装到 NSNumber 中:

NSArray *objects = [NSArray arrayWithObjects:Training_Code, Training_Duration,Training_Startdate,Training_Enddate,[NSNumber numberWithInt:Trainer_ID],Training_Location,Comments,Keyword,[NSNumber numberWithInt:NumberofDays], nil];
于 2013-02-19T15:56:31.623 回答