NSString 转 Json (后台返给的字段key是字符串类型 value是字典类型)

可以根据返回的字典 继续解析 例:model.value = [dic objectForKey@"返回的value值"];

方法:

+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString

{ 

   if (jsonString == nil) {

          return nil;

      }

      NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

      NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];

      return dic;

}

使用:在model中

- (void)setValue:(id)value forUndefinedKey:(NSString *)key

{

    if ([key isEqualToString:@"rewards"]) {

       NSDictionary *dic = [[self class] dictionaryWithJsonString:value];

        self.value = [dic objectForKey:@"value"];

        self.type = [dic objectForKey:@"type"];

    }

}

原文地址:https://www.cnblogs.com/jiangxue-iOS/p/7792281.html