jsonString转NSDictionary


NSData *webData = [ @"{\"1\":
               {\"name\": \"Jerry\",\"age\": \"12\"},
              \"2\":
               {\"name\": \"Bob\",\"age\": \"16\"} }" dataUsingEncoding:NSUTF8StringEncoding ];

NSError *error;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:webData options:0 error:&error];

/*
NSJSONReadingMutableContainers = (1UL << 0),  //  创建可变的数组或字典来接收  0

NSJSONReadingMutableLeaves = (1UL << 1), // 指定在JSON对象可变字符串被创建为NSMutableString的实例 1

NSJSONReadingAllowFragments = (1UL << 2) //  指定解析器应该允许不属于的NSArray或NSDictionary中的实例顶层对象   2

*/
NSLog(@"JSON DIct: %@", jsonDict);
输出如下:

JSON DIct: {
1 = {
age = 12;
name = Jerry;
};
2 = {
age = 16;
name = Bob;
};
}


如果你的jsonString是下面格式的话:

[ {"id":5,"title":"销售员","number":2,"education":1,"money":1,"years":1,"companyname":"腾讯网络技术有限公司","company":1},

{"id":13,"title":"程序员","number":2,"education":1,"money":1,"years":1,"companyname":"腾讯网络技术有限公司","company":1} ]

那调用[NSJSONSerialization JSONObjectWithData:webData options:0 error:&error]返回的就是NSArray, NSArray里面是2个NSDictionary。

jsonString转NSDictionary

时间: 2024-11-15 17:01:05

jsonString转NSDictionary的相关文章

NSString,NSArray和NSDictionary to JSON标准格式字符串

#import <Foundation/Foundation.h> @interface NSString (HXAddtions) +(NSString *) jsonStringWithDictionary:(NSDictionary *)dictionary; +(NSString *) jsonStringWithArray:(NSArray *)array; +(NSString *) jsonStringWithString:(NSString *) string; +(NSStr

iOS NSDictionary &lt;--&gt; NSString(JSON) in Objc

NSDictionary --> NSString + (NSString*)stringINJSONFormatForObject:(id)obj { NSData *jsonData = [NSJSONSerialization dataWithJSONObject:obj options:0 // If that option is not set, the most compact possible JSON will be generated error:nil]; NSString

Swift3 JSON字符串转字典和字典转JSON字符串的实现

直接上代码吧 1.JSONString转换为字典 /// JSONString转换为字典 /// /// - Parameter jsonString: <#jsonString description#> /// - Returns: <#return value description#> func getDictionaryFromJSONString(jsonString:String) ->NSDictionary{ let jsonData:Data = json

wax lua 脚本升级练习(1)

当发现线上app某一功能出现bug,即使在技术上很快的做出了这一bug的补丁,但是因为AppStore上线审核漫长,这将在一段时间里,用户用的都是这个款带有已知bug的产品. 这种情况的一个解决办法就是通过Lua给app打补丁,因为Lua是脚本语言,将脚本放在服务器上,在程序启动的某个时机动态的从服务器上下载Lua代码,并在本地执行,就实现了动态替换app代码,从而快速的给app打补丁. 现在就做一个小练习,模拟一下. (1)功能就是点击"4 x 3 ="按钮,将4和3传到lua,然后

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

1.JSONString转换为字典 // JSONString转换为字典 func getDictionaryFromJSONString(jsonString:String) ->NSDictionary{ let jsonData:Data = jsonString.data(using: .utf8)! let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) if d

NSArray、NSDictionary 转成 JSONString

1.NSArray转换成JSONString //NSArray+JSONString.h @interface NSArray (JSONString) - (NSString *)jsonStringWithPrettyPrint:(BOOL)prettyPrint; @end @implementation NSArray (JSONString) -(NSString*) jsonStringWithPrettyPrint:(BOOL) prettyPrint { NSError *er

当 NSDictionary 遇见 nil

Demo project: NSDictionary-NilSafe 问题 相信用 Objective-C 开发 iOS 应用的人对下面的 crash 不会陌生: *** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1] *** setObjectForKey: key cannot be nil *** setObjectForKey

NSDictionary或NSArray与JSON字符串相互转换

一.将NSDictionary或NSArray转化为JSON串// 将字典或者数组转化为JSON串   NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted  error:nil]; //得到想要的JSON串NSString *jsonString = [[NSString alloc] initWithData:jsonData  encoding:

iOS不使用JSONKit做Dic到JsonString的转换

NSDictionary to jsonString [self DataTOjsonString:dic] -(NSString*)DicToJsonString:(id)object { NSString *jsonString = nil; NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted //Pass 0