我的json字符串是:
jsonData=@"{data:[{\"type\":\"ex\",\"coordinate\":\"19.0760,73.8777\",\"title\":\"Awesome Event 1\"},{\"type\":\"pe\",\"coordinate\":\"19.0760,73.2777\",\"title\":\"Awesome Event 2\"},{\"type\":\"ev\",\"coordinate\":\"19.0760,72.4777\",\"title\":\"Awesome Event 3\"},{\"type\":\"ex\",\"coordinate\":\"19.0760,72.3777\",\"title\":\"Awesome Event 4\"},{\"type\":\"pe\",\"coordinate\":\"19.0760,72.1777\",\"title\":\"Awesome Event 5\"},{\"type\":\"ev\",\"coordinate\":\"19.0760,72.4777\",\"title\":\"Awesome Event 6\"},{\"type\":\"ex\",\"coordinate\":\"19.0760,72.5777\",\"title\":\"Awesome Event 7\"},{\"type\":\"pe\",\"coordinate\":\"19.0760,72.9777\",\"title\":\"Awesome Event 8\"},{\"type\":\"ev\",\"coordinate\":\"19.1760,72.8777\",\"title\":\"Awesome Event 9\"},{\"type\":\"ex\",\"coordinate\":\"19.4760,72.8777\",\"title\":\"Awesome Event 10\"},{\"type\":\"pe\",\"coordinate\":\"19.5760,72.8777\",\"title\":\"Awesome Event 11\"},{\"type\":\"ev\",\"coordinate\":\"19.3760,72.8777\",\"title\":\"Awesome Event 12\"}]}";
如何将其转换为NSDictionary?
答案 0 :(得分:1)
首先将您的jsonString转换为JsonData。根据你的代码,json是字符串格式。请按照以下步骤操作。
- >将jsonString转换为Data - >将数据转换为JsonDict
jsonString to Data
NSData* data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
之后
数据到JsonDict
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
然后,您可以使用键名来访问dict中的值。
答案 1 :(得分:0)
使用NSJSONSerialization类及其方法JSONObjectWithData,您可以将字符串转换为dict或json。
NSData *data = [yourString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// OR
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"string: %@",json);