I have some String values, such as this format,
[INFO] [Tue Aug 21 14:54:22 2012] [ViewController] [26] [Hello] [;]
I want to convert these strings to JSON using NSJSONSerialization.
I use the following code to convert strings,
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);
Then I get the output as follows:
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] [;]"
]
}
but I want it to be so
{
"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]"
}
I do not know how to create a JSON string in the above format, please suggest a solution.
source
share