2013-02-01 100 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);