0
我有這樣一個NSString:ios - 如何從JSON格式的另一個字符串中提取字符串?
[{"comment":"I am just weighing the idea."}]
如何使之成爲一個JSON對象,並獲得評論鍵的值?
謝謝!
我有這樣一個NSString:ios - 如何從JSON格式的另一個字符串中提取字符串?
[{"comment":"I am just weighing the idea."}]
如何使之成爲一個JSON對象,並獲得評論鍵的值?
謝謝!
您可以使用iOS的NSJSONSerialization
對象從JSON字符串/數據獲取對象圖。該API期望一個NSData,所以首先你需要把字符串放入一個。
NSData * jsonData = [myString dataUsingEncoding:NSUTF8StringEncoding];
NSArray * root = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:NULL];
NSString * comment = [[root objectAtIndex:0] objectForKey:@"comment"];
經過處理後,該根對象應該是數組或字典。就你而言,它顯然是一個包含字典的數組。
謝謝你,非常有幫助:) – Genadinik