IOS开发之把 Array 和 Dictionaries 序列化成 JSON 对象
1 前言
通过 NSJSONSerialization 这个类的 dataWithJSONObject:options:error:方法来实现,Array 和 dictionary 序列化成 JSON 对象。方便在网络中传输。
2 代码实例
TestDemo.m
[plain]
(void)converseToJson{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:@"Archy" forKey:@"First Name"];
[dictionary setValue:@"Robbins" forKey:@"Last Name"];
[dictionary setValue:[NSNumber numberWithUnsignedInteger:51] forKey:@"Age"];
NSArray *arrayOfArchysChildren = [[NSArray alloc] initWithObjects:
@"Anthony's Son 1",
@"Anthony's Daughter 1",
@"Anthony's Son 2",
@"Anthony's Son 3",
@"Anthony's Daughter 2", nil];
[dictionary setValue:arrayOfArchysChildren forKey:@"children"];
NSError *error = nil;
//NSJSONWritingPrettyPrinted:指定生成的JSON数据应使用空格旨在使输出更加可读。如果这个选项是没有设置,最紧凑的可能生成JSON表示。
NSData *jsonData = [NSJSONSerialization
dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
if ([jsonData length] > 0 && error == nil){
NSLog(@"Successfully serialized the dictionary into data.");
//NSData转换为String
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON String = %@", jsonString);
}
else if ([jsonData length] == 0 &&
error == nil){
NSLog(@"No data was returned after serialization.");
}
else if (error != nil){
NSLog(@"An error happened = %@", error);
}
}
-(void)converseToJson{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:@"Archy" forKey:@"First Name"];
[dictionary setValue:@"Robbins" forKey:@"Last Name"];
[dictionary setValue:[NSNumber numberWithUnsignedInteger:51] forKey:@"Age"];
NSArray *arrayOfArchysChildren = [[NSArray alloc] initWithObjects:
@"Anthony's Son 1",
@"Anthony's Daughter 1",
@"Anthony's Son 2",
@"Anthony's Son 3",
@"Anthony's Daughter 2", nil];
[dictionary setValue:arrayOfArchysChildren forKey:@"children"];
NSError *error = nil;
//NSJSONWritingPrettyPrinted:指定生成的JSON数据应使用空格旨在使输出更加可读。如果这个选项是没有设置,最紧凑的可能生成JSON表示。
NSData *jsonData = [NSJSONSerialization
dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
if ([jsonData length] > 0 && error == nil){
NSLog(@"Successfully serialized the dictionary into data.");
//NSData转换为String
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON String = %@", jsonString);
}
else if ([jsonData length] == 0 &&
error == nil){
NSLog(@"No data was returned after serialization.");
}
else if (error != nil){
NSLog(@"An error happened = %@", error);
}
}
控制台结果
2013-05-13 17:14:26.087 ToJsonTest[4890:303] Successfully serialized the dictionary into data.
2013-05-13 17:14:26.089 ToJsonTest[4890:303] JSON String = {
"children" : [
"Anthony's Son 1",
"Anthony's Daughter 1",
"Anthony's Son 2",
"Anthony's Son 3",
"Anthony's Daughter 2"
],
"Age" : 51,
"First Name" : "Archy",
"Last Name" : "Robbins"
}
IOS开发之把 Array 和 Dictionaries 序列化成 JSON 对象的更多相关文章
- jQuery表单验证以及将表单序列化为json对象小练习
jquery表单验证(非实时验证),同时,将表单序列化为json对象提交表单. <!DOCTYPE html> <html lang="en"> <h ...
- C#将对象序列化成JSON字符串
C#将对象序列化成JSON字符串 public string GetJsonString() { List<Product> products = new List<Product& ...
- 将表单序列化为JSON对象
将表单序列化为JSON对象的工具方法: $(function() { //工具方法,可以将指定的表单中的输入项目序列化为JSON数据 $.fn.serializeJson = function() { ...
- SpringMVC将表单对象序列化成Json字符串提交,以List接收
出自:http://blog.csdn.net/m0_37595732/article/details/71440853 HTML <%@ page language="java&qu ...
- 023-将表单序列化为json对象
使用jQuery将表单序列化为json对象,其中serializeJson方法的名字任意,serializeArray()这个jQuery提供的方法.this指的就是谁调用了这个方法. $.fn.se ...
- 将序列化成json格式的日期(毫秒数)转成日期格式
<script> $(function () { loadInfo(); }) function loadInfo() { $.post("InfoList.ashx" ...
- 使用 EntityFramework后把一个对象序列化成json字符串引起循环引用的问题
先看一个T4模板生成的model实体类 著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者:卷猫 链接:http://anneke.cn/ArticleInfo/Detial ...
- 将序列化成json格式后日期(毫秒数)转成日期格式
System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaS ...
- C#中类的字段或属性不被序列化成JSON或XML
将一个类序列化成JSON或XML时,如果某个字段或属性不想被序列化,则可以使用以下Attribute: 1.[Newtonsoft.Json.JsonIgnore]特性:使用Newtonsoft.Js ...
随机推荐
- dmalloc在嵌入式的开发板上的应用
下面是我实际在开发环境里面做的dmalloc移植时候的一些随笔 配置PC的dmalloc环境1. 首先把源码包打开,进入dmalloc文件夹2. ./configure 配置Makefile,我是加了 ...
- 【C#】万事开头难(二)<机房重构>
前言 机房将要进行完了,之所以仍然使用这个题目,是由于我想告诉自己.无论面对多么未知的事物.不要害怕,去做就好.在这么多天的机房重构中,发现了好多问题,也攻克了好多问题,今天,就把我解决的问题分享给大 ...
- [Webpack] Access Webpack Dev Server from Mobile Safari on an iPhone
Testing your sites on mobile devices is a critical part of the development process. Webpack dev serv ...
- [Typescript] Sorting arrays in TypeScript
In this lesson we cover all the details of how to sort a list of items using TypeScript. We also pre ...
- php如何实现把多平台文件中所有的行合成一行?
php如何实现把多平台文件中所有的行合成一行? 一.总结 1.str_replace中的数组替换:str_replace(array("/r","/n",&qu ...
- 【u239】整数分解
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 某些数能表示成为一些互不相同的整数的阶乘之和.如9=l!+2! +3!. 现在给定一个非负整数n,要求 ...
- Android 开发者工具
30多个Android 开发者工具 文中部分工具是收费的,但是绝大多数都是免费的. FlowUp 这是一个帮助你跟踪app整体性能的工具,深入分析关键的性能数据如FPS, 内存, CPU, 磁盘, 等 ...
- MKNetWorkKit的使用(1)
在整个程序中只有一个全局队列 MKNetWorkKit中主要有两个类,MKNetworkEngine和 MKNetworkOperation,MKNetworkOperation就是一个操作,是NSO ...
- 三次握手、四次握手、backlog
TCP:三次握手.四次握手.backlog及其他 TCP是什么 首先看一下OSI七层模型: 然后数据从应用层发下来,会在每一层都加上头部信息进行封装,然后再发送到数据接收端,这个基本的流程中每个数 ...
- 一起学Python:多线程-共享全局变量
多线程-共享全局变量 from threading import Thread import time g_num = 100 def work1(): global g_num for i in r ...