首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将JSON字符串转换为字典?

如何将JSON字符串转换为字典?
EN

Stack Overflow用户
提问于 2015-05-27 19:05:33
回答 9查看 288K关注 0票数 210

我想在我的swift项目中创建一个函数,将字符串转换为Dictionary json格式,但我得到了一个错误:

无法转换表达式的类型(@lvalue NSData,选项:整数可转换...

这是我的代码:

func convertStringToDictionary (text:String) -> Dictionary<String,String> {

    var data :NSData = text.dataUsingEncoding(NSUTF8StringEncoding)!
    var json :Dictionary = NSJSONSerialization.JSONObjectWithData(data, options:0, error: nil)
    return json
} 

我在Objective-C中创建了这个函数:

- (NSDictionary*)convertStringToDictionary:(NSString*)string {
  NSError* error;
  //giving error as it takes dic, array,etc only. not custom object.
  NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
  id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  return json;
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30480672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档