我正在 JSON 中获得我的 Web 服务响应之一。这是 UTF8String 基本响应字符串:
"{\"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.}
请帮忙