2013-02-01 93 views
-4

如何添加JSON字符串到NSDictionary?如何添加JSON字符串到NSDictionary

NSString *j_String @"{ "comp": [ 
     { "id": "1" }, 
     { "id": "2" }, 
     { "id": "3" } 
    ], 
    "contact": [ 
     { "mail": "[email protected]", "name": "Name" }, 
     { "mail": "[email protected]", "name": "Name" } 
    ] 
    }"; 

NSMutableDictionary *dic = [NSMutableDictionary dictionary]; 
[dic setValue:@"nvjd" forKey:j_String]; 

我可以这样做将Json字符串添加到NSMutableDictionary或NSDictionary!

+0

转换字符串使用此代码为NSData的,然后用nsjsonseriealization和传递数据将返回字典。 –

+0

你可以使用默认的SBJSON对象 –

回答

1

使用SBJSON将您的Json字符串转换为NSobject。然后添加到您自己的字典中。如果我们想要Json stirng形式,请使用从SBJson Class转换Json writer方法。

0

添加SBJson库在你的项目,你可以从here下载它,比你的.m文件

#import "SBJson.h" 

    SBJsonParser *jsonParser = [SBJsonParser new]; 
    NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString: j_String error:nil]; 

    NSLog(@"json data is : %@",jsonData);