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. 《精通Python设计模式》学习之抽象工厂

    这种工厂模式用得少, 可能在游戏类的编程中用得比较多吧. 这个思路清晰一定要OK的. class Frog: def __init__(self, name): self.name = name de ...

  2. 【Codechef】Random Number Generator(多项式除法)

    题解 前置技能 1.多项式求逆 求\(f(x)\*g(x) \equiv 1 \pmod {x^{t}}\) 我们在t == 1时,有\(f[0] = frac{1}{g[0]}\) 之后呢,我们倍增 ...

  3. codeforces 286 E. Ladies' Shop (FFT)

    E. Ladies' Shop time limit per test 8 seconds memory limit per test 256 megabytes input standard inp ...

  4. mysql 新增时,唯一索引冲突时更新

    INSERT INTO table_name(f1 ,f2 ,f3) VALUES(? ,?) on duplicate key update f2 = ? ,f3 = ?

  5. Gitlab-API各状态码解释

    200 – OK : This means that the GET , PUT, or DELETE request was successful. When you request a resou ...

  6. shell变量定义中的特殊符号

    今天要写一个shell语句来输出数据库的v$database的信息 定义bb为一个接收sql返回值的变量 需要注意的是: select * from v$database ;  语句 由于其中用到了$ ...

  7. C#中 EF(EntityFramework) 性能优化

    现在工作中很少使用原生的sql了,大多数的时候都在使用EF.刚开始的时候,只是在注重功能的实现,最近一段时间在做服务端接口开发.开发的时候也是像之前一样,键盘噼里啪啦的一顿敲,接口秒秒钟上线,但是到联 ...

  8. Ionic实战八:ionic登陆页面源码

    onic登陆页面模板源码,可以用于ionic开发中登陆页面制作或者参考 下面是此模板的一些页面 此模板免费提供给大家  登陆用户名和密码都是 phonegap100     

  9. structs2的action实现方式

    Action的实现方式第一种:在web.xml中添加配置<filter> <filter-name>struts2</filter-name> <filter ...

  10. CentOS 7下MySQL5.7.23的服务配置参数测试

    CentOS 7默认安装MySQL5.7.23,服务管理发生了变化,从sysvinit(service mysql start)变化为systemd(systemctl start mysqld.se ...