2012-08-22 41 views
0

我得到的NSString這樣如何使用jsonkit將數據數組轉換爲json?

[Hello] [;] 
[hi] [;] 
[Its there] [;] 
[Welcome] [;] 
[Hello] [;] 

我想作爲一個JSON格式轉換,我不知道如何將數據的數組轉換成JSON。請幫我

+0

顯示我們輸入的字符串,你已經寫了這樣做的代碼。 –

回答

2

你需要使用下面的代碼。

NSDictionary *dict = [myJsonString JSONValue]; 

如果它不工作,然後使用後

// 
// we begin with our string in json format 
// 
NSString *jsonString = [[NSString alloc] initWithString:@"{\"1\":\"1: Rossy Robinson - $25\",\"2\":\"7: Davey Ambrose - $25\",\"3\":\"14: Ross Robinson - $25\"}"]; 

// 
// convert the json string to an NSMutableDictionary 
// 
NSError *e; 
NSMutableDictionary *JSONdic = [NSJSONSerialization JSONObjectWithData: [jsonString dataUsingEncoding: NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error: &e]; 

// 
// change a value and add a new value in the dict 
// 
NSLog(@"before: object for key 1 is: %@", [JSONdic objectForKey:@"1"]); 
[JSONdic setObject:@"xxx" forKey:@"1"]; 
[JSONdic setObject:@"Phil McQuitty" forKey:@"2"]; 

// 
//convert dictionary object to json data 
// 
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:JSONdic options:NSJSONWritingPrettyPrinted error:&e]; 

// 
// convert the json data back to a string 
// 
NSString *jsonText = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];\ 

// 
// print out the final results 
// 
NSLog(@"back to string: %@", jsonText); 
+0

這個程序行對我非常有用 –

+0

謝謝@YuvarajM –

+0

我有紡織品的數據,我如何分配給objectkey以及如何轉換成json格式,用於將值存儲到服務器中。 –