[摘要]本文是對NSString,NSArray和NSDictionary to JSON標准格式字符串的講解,對學習IOS蘋果軟件開發有所幫助,與大家分享。
#import<Foundation/Foundation.h>
@interfaceNSString (HXAddtions)
+(NSString *) jsonStringWithDictionary:(NSDictionary *)dictionary;
+(NSString *) jsonStringWithArray:(NSArray *)array;
+(NSString *) jsonStringWithString:(NSString *) string;
+(NSString *) jsonStringWithObject:(id) object;
+(void) jsonTest;
@end
//
// NSString+HXAddtions.m
// HXWeb
//
// Created by hufeng on 12-2-13.
// Copyright (c) 2012年__MyCompanyName__. All rights reserved.
//
#import"NSString+HXAddtions.h"
@implementationNSString (HXAddtions)
+(void)jsonTest{
//test
NSDictionary *dictionary1 = [NSDictionary dictionaryWithObjectsAndKeys:
@"阿三\"\n11",@"name",
@"18",@"age",
nil];
NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjectsAndKeys:
@"阿四",@"name",
@"20",@"age",
nil];
NSArray *array = [NSArray arrayWithObjects:dictionary1,dictionary2,nil];
NSDictionary *dictionary3 = [NSDictionary dictionaryWithObjectsAndKeys:
@"李\na",@"name",
@"29",@"age",
nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"string",@"string",
array,@"array",
dictionary3,@"dictionary",
nil];
NSLog(@"dictionary:%@",dictionary);
NSString *jsonString = [NSString jsonStringWithObject:dictionary];
NSLog(@"dictionary jsonString:%@",jsonString);
NSLog(@"%@",[NSDictionary dictionaryWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]]);
}
+(NSString *) jsonStringWithString:(NSString *) string{
return[NSString stringWithFormat:@"\"%@\"",
[[string stringByReplacingOccurrencesOfString:@"\n"withString:@"\\n"] stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""]
];
}
+(NSString *) jsonStringWithArray:(NSArray *)array{
NSMutableString *reString = [NSMutableString string];
[reString appendString:@"["];
NSMutableArray *values = [NSMutableArray array];
for(idvalueObjinarray) {
NSString *value = [NSString jsonStringWithObject:valueObj];
if(value) {
[values addObject:[NSString stringWithFormat:@"%@",value]];
}
}
[reString appendFormat:@"%@",[values componentsJoinedByString:@","]];
[reString appendString:@"]"];
returnreString;
}
+(NSString *) jsonStringWithDictionary:(NSDictionary *)dictionary{
NSArray *keys = [dictionary allKeys];
NSMutableString *reString = [NSMutableString string];
[reString appendString:@"{"];
NSMutableArray *keyValues = [NSMutableArray array];
for(inti=0; i<[keys count]; i++) {
NSString *name = [keys objectAtIndex:i];
idvalueObj = [dictionary objectForKey:name];
NSString *value = [NSString jsonStringWithObject:valueObj];
if(value) {
[keyValues addObject:[NSString stringWithFormat:@"\"%@\":%@",name,value]];
}
}
[reString appendFormat:@"%@",[keyValues componentsJoinedByString:@","]];
[reString appendString:@"}"];
returnreString;
}
+(NSString *) jsonStringWithObject:(id) object{
NSString *value =nil;
if(!object) {
returnvalue;
}
if([object isKindOfClass:[NSString class]]) {
value = [NSString jsonStringWithString:object];
}elseif([object isKindOfClass:[NSDictionary class]]){
value = [NSString jsonStringWithDictionary:object];
}elseif([object isKindOfClass:[NSArray class]]){
value = [NSString jsonStringWithArray:object];
}
returnvalue;
}
@end