如何将UTF8String转换为基于JSON的NSDictionary?

时间:2013-09-24 14:55:44

标签: ios json nsstring nsjsonserialization

我在JSON中获得了一个网络服务响应。这是UTF8String基本responseString:

"{\"ApplicationId\":1,\"CurrentPosition\":0,\"EndOfFile\":false,\"Media\":{\"Active\":true,\"Description\":\"This video demonstrates a quick overview of Bentley LEARN Server.\",\"LocationId\":1,\"MP4URL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"MediaId\":5003235,\"MediaURL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"StreamServer\":null,\"Title\":\"Welcome\",\"TransferStatus\":0,\"filesize\":9094429},\"MediaDuration\":0,\"SkippedCurrentPosition\":0,\"UserBP\":8011512,\"ViewedTime\":0}"

格式化后,它看起来像:

{
 "ApplicationId":1,
 "CurrentPosition":0,
 "EndOfFile":false,
 "Media":
  {
   "Active":true,
   "Description":"This video demonstrates a quick overview of Bentley LEARN Server.",
   "LocationId":1,
   "MP4URL":"http:\/\/cdnllnw.bentley.com\/s\/LearnVideo\/Welcome.mp4?s=1380035311&e=1380078511&h=d8de8ade046266fb7324be90a575f978",
   "MediaId":5003235,
   "MediaURL":"http:\/\/cdnllnw.bentley.com\/s\/LearnVideo\/Welcome.mp4?s=1380035311&e=1380078511&h=d8de8ade046266fb7324be90a575f978",
   "StreamServer":null,
   "Title":"Welcome",
   "TransferStatus":0,
   "filesize":9094429
  },
 "MediaDuration":0,
 "SkippedCurrentPosition":0,
 "UserBP":8011512,
 "ViewedTime":0
 }

如何将此字符串转换为JSON对象或NSDictionary对象,以便我可以轻松访问所有键值。

我试过这个:

NSString *responseString = [NSString stringWithUTF8String:response.c_str()];


NSDictionary *JSONdict =
[NSJSONSerialization JSONObjectWithData: [responseString dataUsingEncoding:NSUTF8StringEncoding]
                                options: NSJSONReadingMutableContainers
                                  error: &error];

但是它说JSONdict是nil,错误描述是:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0xd8bbd30 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

请帮忙

2 个答案:

答案 0 :(得分:1)

根据错误添加NSJSONReadingAllowFragments选项应该有所帮助:

NSString* jsonString = @"{\"ApplicationId\":1,\"CurrentPosition\":0,\"EndOfFile\":false,\"Media\":{\"Active\":true,\"Description\":\"This video demonstrates a quick overview of Bentley LEARN Server.\",\"LocationId\":1,\"MP4URL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"MediaId\":5003235,\"MediaURL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"StreamServer\":null,\"Title\":\"Welcome\",\"TransferStatus\":0,\"filesize\":9094429},\"MediaDuration\":0,\"SkippedCurrentPosition\":0,\"UserBP\":8011512,\"ViewedTime\":0}";

NSError* error = nil;
NSDictionary *JSONdict =
[NSJSONSerialization JSONObjectWithData: [responseString dataUsingEncoding:NSUTF8StringEncoding]
                            options: NSJSONReadingMutableContainers | NSJSONReadingAllowFragments
                              error: &error];

但这意味着您在问题中粘贴的NSString包含不遵循JSON格式的额外字符。如果您从获取此值(文件,Web)的位置添加更多代码 - 我可以帮助更多以使其有效。在此之前,将此答案视为解决方法:)

格式正确的字符串应为:

{\"ApplicationId\":1,\"CurrentPosition\":0,\"EndOfFile\":false,\"Media\":{\"Active\":true,\"Description\":\"This video demonstrates a quick overview of Bentley LEARN Server.\",\"LocationId\":1,\"MP4URL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"MediaId\":5003235,\"MediaURL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"StreamServer\":null,\"Title\":\"Welcome\",\"TransferStatus\":0,\"filesize\":9094429},\"MediaDuration\":0,\"SkippedCurrentPosition\":0,\"UserBP\":8011512,\"ViewedTime\":0}

没有“在开始和结束时。也许这就是原因。 如果您从网上加载 - 请确保您没有任何JS包含或额外标题,并确认您的Web服务在标题中以JSON格式响应。

答案 1 :(得分:1)

我尝试使用下一段代码(这是一个起点)从“媒体”输出字典的NSString;

NSString *string = @"{\"ApplicationId\":1,\"CurrentPosition\":0,\"EndOfFile\":false,\"Media\":{\"Active\":true,\"Description\":\"This video demonstrates a quick overview of Bentley LEARN Server.\",\"LocationId\":1,\"MP4URL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"MediaId\":5003235,\"MediaURL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"StreamServer\":null,\"Title\":\"Welcome\",\"TransferStatus\":0,\"filesize\":9094429},\"MediaDuration\":0,\"SkippedCurrentPosition\":0,\"UserBP\":8011512,\"ViewedTime\":0}";

NSString *newString = [string stringByReplacingOccurrencesOfString:@"\\" withString:@""];

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[newString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];

NSLog(@"%@", [dictionary valueForKey:@"Media"]);

结果是:

{
Active = 1;
Description = "This video demonstrates a quick overview of Bentley LEARN Server.";
LocationId = 1;
MP4URL = "http://cdnllnw.bentley.com/s/LearnVideo/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a";
MediaId = 5003235;
MediaURL = "http://cdnllnw.bentley.com/s/LearnVideo/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a";
StreamServer = "<null>";
Title = Welcome;
TransferStatus = 0;
filesize = 9094429;
}

希望它对你有用。