我有一些像这种格式的字符串值,
[INFO] [Tue Aug 21 14:54:22 2012] [ViewController] [26] [Hello] [;]
我想使用 NSJSONSerialization 将这些字符串转换为 JSON。
我正在使用以下代码来转换字符串,
for (i = 0; i < [logArray count]; i++)
{
individualLogInfoArray = [[logArray objectAtIndex:i] componentsSeparatedByString:kDelimitterSpace];
[dictionaryArray addObject:individualLogInfoArray];
}
finalLogDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:dictionaryArray,@"Log", nil];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:finalLogDictionary
options:NSJSONWritingPrettyPrinted
error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JsonString = %@",jsonString);
然后我得到这样的输出,
JsonString = {
"Log" : [
"[INFO] [Tue Aug 21 14:54:22 2012] [ViewController] [26] [Hello] [;]",
"[DEBUG] [Tue Aug 21 14:54:22 2012] [ViewController] [27] [hi] [;]",
"[INFO] [Tue Aug 21 14:54:22 2012] [ViewController] [28] [Its there] [;]",
"[PROD] [Tue Aug 21 14:54:22 2012] [ViewController] [29] [Welcome] [;]"
]
}
但我想要这样的输出,
{
"log": "[INFO] [Tue Aug 21 14:54:22 2012] [ViewController] [26] [Hello],[INFO] [Tue Aug 21 14:54:22 2012] [ViewController] [26] [Hello],[INFO] [Tue Aug 21 14:54:22 2012] [ViewController] [26] [Hello]"
}
我不知道如何生成上述格式的 JSON 字符串,请提出解决方案。