JSON

JSON相关的,数据彼此间的转化进行了简单地封装,源码如下,支持arc与非arc

YXJSON.h + YXJSON.m

//
// YXJSON.h
//
// JSONString 与 JSONData 与 字典或者数组互相转化
//
// Copyright (c) 2014年 YouXian. All rights reserved.
// #import <Foundation/Foundation.h> @interface YXJSON : NSObject /**
将字典或者数组转换为JSON格式字符串 @return JSON格式字符串
*/
+ (NSString *)JSONStringWithDictionaryOrArray:(id)dictionaryOrArray; /**
将字典或者数组转换为JSON的Data @return JSON的Data
*/
+ (NSData *)JSONSDataWithDictionaryOrArray:(id)dictionaryOrArray; /**
将JSON格式字符串转换为字典或者数组 @return 字典或者数组
*/
+ (id)dictionaryOrArrayWithJSONSString:(NSString *)jsonString; /**
将JSON的Data转换为字典或者数组 @return 字典或者数组
*/
+ (id)dictionaryOrArrayWithJSONSData:(NSData *)jsonData; @end
//
// YXJSON.m
//
// JSONString 与 JSONData 与 字典或者数组互相转化
//
// Copyright (c) 2014年 YouXian. All rights reserved.
// #if __has_feature(objc_arc)
#define YX_release(obj)
#define YX_autorelease(obj)
#else
#define YX_release(obj) [obj release]
#define YX_autorelease(obj) [obj autorelease]
#endif #import "YXJSON.h" @implementation YXJSON + (NSString *)JSONStringWithDictionaryOrArray:(id)dictionaryOrArray
{
if (dictionaryOrArray == nil)
{
return nil;
} //options: Pass 0 if you don't care about the readability of the generated string
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionaryOrArray
options:NSJSONWritingPrettyPrinted
error:nil]; if (data == nil)
{
return nil;
} NSString *string = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
YX_autorelease(string); return string;
} + (NSData *)JSONSDataWithDictionaryOrArray:(id)dictionaryOrArray
{
if (dictionaryOrArray == nil)
{
return nil;
} //options: Pass 0 if you don't care about the readability of the generated string
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionaryOrArray
options:NSJSONWritingPrettyPrinted
error:nil];
return data;
} + (id)dictionaryOrArrayWithJSONSString:(NSString *)jsonString
{
if (jsonString == nil)
{
return nil;
} NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; return [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableLeaves|| NSJSONReadingMutableContainers
error:nil];
} + (id)dictionaryOrArrayWithJSONSData:(NSData *)jsonData
{
if (jsonData == nil)
{
return nil;
} return [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableLeaves|| NSJSONReadingMutableContainers
error:nil];
} @end

没有将其单独的封装到 NSString NSData NSArray NSDictionary 相关类目中,实际上用类目的方式使用更加方便,有需求可以自己弄,但文件多较为繁琐,本人不习惯.

JSONString 与 JSONData 与字典或者数组互相转化的更多相关文章

  1. Swift JSON字符串和字典以及数组的互转

    1.JSONString转换为字典 // JSONString转换为字典 func getDictionaryFromJSONString(jsonString:String) ->NSDict ...

  2. iOS 字典或者数组和JSON串的转换

    在和服务器交互过程中,会iOS 字典或者数组和JSON串的转换,具体互换如下: // 将字典或者数组转化为JSON串 + (NSData *)toJSONData:(id)theData { NSEr ...

  3. 使用JavaScriptSerializer序列化集合、字典、数组、DataTable为JSON字符串 分类: 前端 数据格式 JSON 2014-10-30 14:08 169人阅读 评论(0) 收藏

    一.JSON简介 JSON(JavaScript Object Notation,JavaScript对象表示法)是一种轻量级的数据交换格式. JSON是"名值对"的集合.结构由大 ...

  4. IOS_FMDB有关字典、数组存储及获取问题

    http://blog.csdn.net/betterbb/article/details/25984455 FMDB存储字典或数组时会变成字符串存入sqlite里,但如果不将其转换成json格式存储 ...

  5. iOS开发小技巧--字典和数组的中文输出

    一.在解析json数据的时候,得到的集合对象或者数组对象在用%@打印的时候回出现类似乱码的情况.如图: 在iOS中打印字典或者数组对象,系统会默认调用字典对象和数组对象的descriptionWith ...

  6. 将字典或者数组转换成JSON数据或者字符串

    将字典或者数组转换成JSON数据或者字符串 源码: NSDictionary+JSON.h 与 NSDictionary+JSON.m // // NSDictionary+JSON.h // Cat ...

  7. 使用 NSPropertyListSerialization 持久化字典与数组

    NSPropertyListSerialization The NSPropertyListSerialization class provides methods that convert prop ...

  8. shell进阶篇之字典和数组结合应用案例

    # 现在我们用字典结合数组来实现一个简单的远程管理机 远程管理机的需求:现在需要在一个管理机上实现下列两点内容: 1.需要可以实时查看现有项目运行状态 2.远程登陆任意一台机器 备注:现有的机器如下 ...

  9. python字典键值对转化为相应的变量名和变量值

    将python字典键值对转化为相应的变量名和变量值可以使用以下方法: globals().update({"name":"value"}) locals().u ...

随机推荐

  1. USACO 6.2 Shaping Regions

    Shaping Regions N opaque rectangles (1 <= N <= 1000) of various colors are placed on a white s ...

  2. 【Java】 int与String类型间的相互转化

    public class Test { public static void main(String[] args) { /* * int类型转String类型 */ int n1 = 9; //1. ...

  3. Dubbo中只订阅与只注册

    一:只订阅 1.场景 为方便开发测试,经常会在线下共用一个所有服务可用的注册中心,这时,如果一个正在开发中的服务提供者注册,可能会影响消费者不能正常运行. 可以让服务提供者开发方,只订阅服务(开发的服 ...

  4. grep、find命令整理

    一.grep格式: grep [选项]... PATTERN [FILE]...(默认的PATTERN是一个基本的正则表达式(BRE)) 参数选项 1.杂项: -s, --no-messages 不显 ...

  5. annotation中的Autowired

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  6. Python并发编程-IO模型-IO多路复用实现SocketServer

    Server.py import select import socket sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.setblockin ...

  7. LINUX 下挂载 exfat 格式 u 盘或移动硬盘

    apt-get update apt-get install exfat-utils

  8. Python如何将RGB图像转换为Pytho灰度图像?

    我正尝试使用matplotlib读取RGB图像并将其转换为灰度.在matlab中,我使用这个: 1 img = rgb2gray(imread('image.png')); 在matplotlib t ...

  9. 【BZOJ 2721】 2721: [Violet 5]樱花 (筛)

    2721: [Violet 5]樱花 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 599  Solved: 354 Description Input ...

  10. java 反编译 android 反编译

    1. jad http://varaneckas.com/jad/jad158e.linux.intel.zip  下载jad, 给jad运行权限 ,运行 chmod a+x ./jad ./jad ...