我有一些像这种格式的字符串值,
[INFO] [Tue Aug 21 14:54:22 2012] [ViewController] [26] [Hello] [;]
Run Code Online (Sandbox Code Playgroud)
我想使用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);
Run Code Online (Sandbox Code Playgroud)
然后我得到这样的输出,
JsonString = {
"Log" : [
"[INFO] [Tue Aug 21 14:54:22 2012] [ViewController] [26] [Hello] [;]",
"[DEBUG] [Tue Aug 21 14:54:22 2012] [ViewController] [27] …Run Code Online (Sandbox Code Playgroud) 我对块的概念有点熟悉,但我不知道如何在iOS5中创建它们.任何人都可以给我一个简单的例子吗?非常感激 :)