一、将NSDictionary或NSArray转化为JSON串 // 将字典或者数组转化为JSON串 - (NSData *)toJSONData:(id)theData{ NSError *error = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData options:NSJSONWritingPrettyPrinted error: if
一、将NSDictionary或NSArray转化为JSON串
// 将字典或者数组转化为JSON串
- (NSData *)toJSONData:(id)theData{
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData
options:NSJSONWritingPrettyPrinted
error:&error];
if ([jsonData length] > 0 && error == nil){
return jsonData;
}else{
return nil;
}
}
使用这个方法的返回,我们就可以得到想要的JSON串
NSString *jsonString = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
二、将JSON串转化为NSDictionary或NSArray
将NSString转化为NSData
[jsonString dataUsingEncoding:NSASCIIStringEncoding];
// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
NSError *error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingAllowFragments
error:&error];
if (jsonObject != nil && error == nil){
return jsonObject;
}else{
// 解析错误
return nil;
}
}
三、将JSON串与NSArray和NSDictionary的操作进行封装
当然,也有很多时候,我们将这些操作,分别定义在NSObject和NSString的一个分类中
直接贴:
1.将NSString转化为NSArray或者NSDictionary
#import "NSString+JSONCategories.h"
@implementation NSString(JSONCategories)
-(id)JSONValue;
{
NSData* data = [self dataUsingEncoding:NSUTF8StringEncoding];
__autoreleasing NSError* error = nil;
id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error != nil) return nil;
return result;
}
@end
2.将NSArray或者NSDictionary转化为NSString
#import "NSObject+JSONCategories.h"
@implementation NSObject (JSONCategories)
-(NSData*)JSONString;
{
NSError* error = nil;
id result = [NSJSONSerialization dataWithJSONObject:self
options:kNilOptions error:&error];
if (error != nil) return nil;
return result;
}
@end
一、将NSDictionary或NSArray转化为JSON串 // 将字典或者数组转化为JSON串 - (NSData *)toJSONData:(id)theData{ NSError *error = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData options:NSJSONWritingPrettyPrinted error:&error]; if ([jsonData length] > 0 && error == nil){ return jsonData; }else{ return nil; } } 使用这个方法的返回,我们就可以得到想要的JSON串 NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 二、将JSON串转化为NSDictionary或NSArray 将NSString转化为NSData [jsonString dataUsingEncoding:NSASCIIStringEncoding]; // 将JSON串转化为字典或者数组 - (id)toArrayOrNSDictionary:(NSData *)jsonData{ NSError *error = nil; id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error]; if (jsonObject != nil && error == nil){ return jsonObject; }else{ // 解析错误 return nil; } } 三、将JSON串与NSArray和NSDictionary的操作进行封装 当然,也有很多时候,我们将这些操作,分别定义在NSObject和NSString的一个分类中 直接贴: 1.将NSString转化为NSArray或者NSDictionary #import "NSString+JSONCategories.h" @implementation NSString(JSONCategories) -(id)JSONValue; { NSData* data = [self dataUsingEncoding:NSUTF8StringEncoding]; __autoreleasing NSError* error = nil; id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; if (error != nil) return nil; return result; } @end 2.将NSArray或者NSDictionary转化为NSString #import "NSObject+JSONCategories.h" @implementation NSObject (JSONCategories) -(NSData*)JSONString; { NSError* error = nil; id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error]; if (error != nil) return nil; return result; } @end

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.