字符串转换成json数据再转换成字典(字符串相关操作和json解析)

1.数据格式(字符串)

ExtraData={"type":"1","id":"10030101","title":"我是广告", "oglink":"http%3A%2F%2Fwww.baidu.com", "link":"http://dol.tianya.cn/c?z=tianya&la=0&si=763&cg=717&c=10030101&ci=231&or=564&l=16320&bg=16320&b=16387&u=http%3A%2F%2Fwww.baidu.com", "pic":"http://801.tianya.cn/dolphin/tianya/2016/02/0_502.jpg", "logo":"1", "istrack":"1"};ExtraData={"type":"1","id":"10030101","title":"我是广告", "oglink":"http%3A%2F%2Fwww.baidu.com", "link":"http://dol.tianya.cn/c?z=tianya&la=0&si=763&cg=717&c=10030101&ci=231&or=564&l=16320&bg=16320&b=16387&u=http%3A%2F%2Fwww.baidu.com", "pic":"http://801.tianya.cn/dolphin/tianya/2016/02/0_502.jpg", "logo":"1", "istrack":"1"};

2.分析数据

数据类似json数据但是其实不是。我们发现数据还是相对标准。

(1)我们可以先把字符串中的"ExtraData="代替为空字符串"":theString等于上面的一整串字符串。

NSString*strResult= [theStringstringByReplacingOccurrencesOfString:@"ExtraData="withString:@""];

NSLog(@"strResult....%@",strResult);

strResult输出结果为:

{"type":"1","id":"10030101","title":"我是广告", "oglink":"http%3A%2F%2Fwww.baidu.com", "link":"http://dol.tianya.cn/c?z=tianya&la=0&si=763&cg=717&c=10030101&ci=231&or=564&l=16320&bg=16320&b=16387&u=http%3A%2F%2Fwww.baidu.com", "pic":"http://801.tianya.cn/dolphin/tianya/2016/02/0_502.jpg", "logo":"1", "istrack":"1"};{"type":"1","id":"10030101","title":"我是广告", "oglink":"http%3A%2F%2Fwww.baidu.com", "link":"http://dol.tianya.cn/c?z=tianya&la=0&si=763&cg=717&c=10030101&ci=231&or=564&l=16320&bg=16320&b=16387&u=http%3A%2F%2Fwww.baidu.com", "pic":"http://801.tianya.cn/dolphin/tianya/2016/02/0_502.jpg", "logo":"1", "istrack":"1"};

(2)输出结果类似于两个字典,我们只要用字符串的提取方法把字符串从”;”分割出来组成一个数组:(注意:这里组成的数组的元素并不是字典,只是字符串,而且最后一个”;”后边也有一个空的字符串“”,不能忽略)

NSArray*array = [strResultcomponentsSeparatedByString:@";"];

array的结果如下:

("{"type":"1","id":"10030101","title":"我是广告", "oglink":"http%3A%2F%2Fwww.baidu.com", "link":"http://dol.tianya.cn/c?z=tianya&la=0&si=763&cg=717&c=10030101&ci=231&or=564&l=16320&bg=16320&b=16387&u=http%3A%2F%2Fwww.baidu.com", "pic":"http://801.tianya.cn/dolphin/tianya/2016/02/0_502.jpg", "logo":"1", "istrack":"1”}","{"type":"1","id":"10030101","title":"我是广告", "oglink":"http%3A%2F%2Fwww.baidu.com", "link":"http://dol.tianya.cn/c?z=tianya&la=0&si=763&cg=717&c=10030101&ci=231&or=564&l=16320&bg=16320&b=16387&u=http%3A%2F%2Fwww.baidu.com", "pic":"http://801.tianya.cn/dolphin/tianya/2016/02/0_502.jpg", "logo":"1", "istrack":"1”}","")

(3)数组的部分数据(除了最后一个)是json数据,就可以用json数据解析

NSString*jsonString = array[0];

NSData*jsonData = [jsonStringdataUsingEncoding:NSUTF8StringEncoding];

NSError*err;

NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:jsonDataoptions:NSJSONReadingMutableContainerserror:&err];

输出经过json解析的字典dic:

{

id = 10030101;

istrack = 1;

link = "http://dol.tianya.cn/c?z=tianya&la=0&si=763&cg=717&c=10030101&ci=231&or=564&l=16320&bg=16320&b=16387&u=http%3A%2F%2Fwww.baidu.com";

logo = 1;

oglink = "http%3A%2F%2Fwww.baidu.com";

pic = "http://801.tianya.cn/dolphin/tianya/2016/02/0_502.jpg";

title = "\U6211\U662f\U5e7f\U544a";

type = 1;

}

你可能感兴趣的:(字符串转换成json数据再转换成字典(字符串相关操作和json解析))