Json字符串与字典对象的相互转化

Json字符串转对象:

    NSString *jsonStringSource = @"{\"name\":\"张三\",\"age\":\"10\"}";

    NSData *jsonDataSource = [jsonStringSource dataUsingEncoding:NSUTF8StringEncoding];

    NSError *error;

    NSDictionary *jsonObj = [NSJSONSerialization JSONObjectWithData:jsonDataSource options:NSJSONReadingAllowFragments error:&error];


字典对象转Json字符串:

    NSData *jsonDataTarget = [NSJSONSerialization dataWithJSONObject:d options:NSJSONWritingPrettyPrinted error:&error];

    NSString *jsonStringTarget = [[NSString alloc]initWithData:jsonDataTarget encoding:NSUTF8StringEncoding];


数据结果据如下:

Json字符串与字典对象的相互转化_第1张图片

你可能感兴趣的:(Json字符串与字典对象的相互转化)