0

我在 NS 数组中得到如下 JSON 数据:

看来这不是有效的 JSON

jsonArray:
{
    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}]";
}

我想将其更改为有效的 json,如下所示:

[
{
    "Training_Code": "1234",
    "Training_Duration": "2hrs",
    "Training_Startdate": "14/02/201315: 00: 00",
    "Training_Enddate": "14/02/201317: 00: 00",
    "Trainer_ID": 1,
    "Training_Location": "B-WingTrainingroom-4",
    "Comments": "C#training",
    "Keyword": "C#1234",
    "NumberofDays": 1
},
{
    "Training_Code": "4321",
    "Training_Duration": "16",
    "Training_Startdate": "17/02/201310: 30: 00",
    "Training_Enddate": "17/02/201317: 30: 00",
    "Trainer_ID": 2,
    "Training_Location": "A-WingTrainingRoom-6",
    "Comments": "Objective-C",
    "Keyword": "Obj-C4321",
    "NumberofDays": 2
}

]

注意:我不知道这个“d”是从哪里来的……请记住这一点。

如何更改为有效的 json 并将其插入到我的 Sqlite DB 中?谢谢。

4

3 回答 3

2

您始终可以将其插入为纯文本。如果你想操作 JSON 字符串,我推荐这个. 您可以将该字符串转换为 JKArray(与数组相同)。之后,遍历您的数组并执行您的数据库操作(插入您的表中......)

我错过了什么吗?也许我需要更多关于你想做什么的信息......

于 2013-02-20T08:42:31.117 回答
0

使用您的列映射键来解析您的 jsonNSJSONSerialization并插入到数据库中。

    NSString *str = [[NSString alloc] initWithString:@"[{\"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}]"];

    NSError *jsonError = nil;
    id allValues = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding]
                                                   options:0
                                                     error:&jsonError];

    if(jsonError!=nil)
        InfoLog(@"error: %@",jsonError);

    NSArray *result = (NSArray*)allValues;

    for(int i=0;i<[result count];i++)
    {
            NSDictionary *values = (NSDictionary*)[result objectAtIndex:i];
            NSLog(@"Training_Code: %@  Training_Duration: %@",[values objectForKey:@"Training_Code"],[values objectForKey:@"Training_Duration"]);
    }

现在您可以从 NSDictionary 获取值,然后简单地添加到您的数据库中。

于 2013-02-20T08:42:49.487 回答
0

这是一个包含编码 JSON 数组的字符串。您需要使用 JSON 解码器(从 iOS 5 开始包含电池)将其转换为 NSArray,然后遍历它:

以下(未经测试的)代码应该是正确的:

// Assuming jsonArray is an object with an NSString property, d...
NSData *data = [jsonArray.d dataUsingEncoding:NSUTF8StringEncoding];

NSError *err;
NSArray *d = [NSJSONSerialization JSONObjectWithData:data options:0 error:err];
// Check for errors.
for (NSDictionary *row in d) {
    NSString *trainingCode = [row objectForKey:@"Training_Code"];
    …
    // Insert into SQLite here.
}

请注意,在最新版本的 Xcode 中,您可以编写row[@"Training_Code"]而不是[row objectForKey:@"Training_Code"].

于 2013-02-20T08:54:13.327 回答