NSDictionary+JSON - iOS

日常开发中常用的一个相互转换的方法;

直接创建对应的类,引用如下方法即可实现;

具体 code 如下:

声明:

#import <Foundation/Foundation.h>

@interface NSDictionary (JSON)

- (NSString *)jsonString;

- (void)writeToJsonFile:(NSString *)path atomically:(BOOL)atomically;

+ (NSDictionary *)dictionaryWithContentsOfJsonFile:(NSString *)path;

@end

实现:

#import "NSDictionary+JSON.h"
#import "NSString+JSON.h"

@implementation NSDictionary (JSON)

- (NSString *)jsonString {
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
                                                       options:0
                                                         error:&error];
    if (error)
        return nil;
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}

- (void)writeToJsonFile:(NSString *)path atomically:(BOOL)atomically {
    NSData *content = [[self jsonString] dataUsingEncoding:NSUTF8StringEncoding];
    [content writeToFile:path atomically:atomically];
}

+ (NSDictionary *)dictionaryWithContentsOfJsonFile:(NSString *)path {
    NSError *error = nil;
    NSString *content = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        return nil;
    }
    id result = [content jsonObject];
    if (![result isKindOfClass:[self class]]) {
        return nil;
    }
    return result;
}

@end

  

注:实现类中需要引入的类在如下飞机票中,具体详情请跳转查看.

NSString+JSON - iOS 机票如下: NSString+JSON - iOS

以上便是此次分享的内容,希望能对大家有所帮助!

原文地址:https://www.cnblogs.com/survivorsfyh/p/9670238.html

时间: 2024-08-30 13:53:03

NSDictionary+JSON - iOS的相关文章

NSString+JSON - iOS

日常开发中常用的一个相互转换的方法; 直接创建对应的类,引用如下方法即可实现; 具体 code 如下: 声明: #import <Foundation/Foundation.h> @interface NSString (JSON) - (id)jsonObject; /** dic -> json*/ - (NSString *)dictionaryToJson:(NSDictionary *)dic; @end 实现: #import "NSString+JSON.h&qu

JSON TO NSDictionary Mac & iOS

NSString * jsonPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Contents.json"]; NSString * jsonStr=[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:jsonPath] encoding:NSUTF8StringEncoding]; NSLog(@"%

iOS NSDictionary、NSData、JSON等 数据类型相互转换

1.NSDictionary类型转换为NSData类型: NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"balance", @"key", @"remaining balance", @"label", @"45", @"value", @"USD", @"curre

iOS NSDictionary转化为Json

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #929292 } span.s1 { } NSDictionary (JSON).h文件 #import <Foundation/Foundation.h> @interface NSDictionary (JSON) /** * 转换成JSON串字符串(没有可读性) * * @return JSON字符串 */ - (NSString *)toJSONStr

iOS开发网络篇—发送json数据给服务器以及多值参数

iOS开发网络篇—发送json数据给服务器以及多值参数 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 (2)设置请求头 (3)设置JSON数据为请求体 代码示例: 1 #import "YYViewController.h" 2 3 @interface YYViewController () 4 5 @end 6 7 @implementation YYViewController 8 9 - (void)viewDidLoad 10

Working with JSON in iOS

Note from author: This is a learning note of Working with JSON in iOS 5 Tutorial written by Ray Wenderlich. JSON is a simple human readable format that is often used to send data over a newtork connection. For instance, if you have a Pet object with

IOS JSON格式转换

- (IBAction)checkUpdate:(id)sender {          NSString *[email protected]"http://www.dszpvip.com/index.php?ios/api";          NSURL * url=[NSURL URLWithString:urlStr];     NSURLRequest * request=[[NSURLRequest alloc]initWithURL:url];     NSError

iOS Json转换模型库:YYModel

iOS Json转换模型库:YYModel 其实在研究这个库之前,市面上已经有很多类似的模型序列化成JSON及反序列化库(如Mantle.MJExtension)了,推荐他只是因为他高端的性能和容错(错误对象类型赋值到属性时YYMODEL会尝试自动转换,避免Crash)以及低侵入(不需要你的MODEL类去继承某个基类.因为他是Category 方式来实现的).作者号称对比性能如下: 接下来直接写一个小例子看如何使用: 1.首先准备JSON及对象如下: {    "userName":

[iOS 多线程 &amp; 网络 - 2.6] - 使用POST上传JSON数据 &amp; 多值参数

A.上传JSON 1.思路: 必须使用POST方法才能上传大量JSON数据 设置请求头:设置Content-Type 设置请求体,JSON实际相当于字典,可以用NSDictionary NSJSONSerialization把字典数据转换成JSON二进制 2.实现 1 // 2 // ViewController.m 3 // PostJsonDemo 4 // 5 // Created by hellovoidworld on 15/1/28. 6 // Copyright (c) 2015年