簡體   English   中英

ios - 如何從另一個JSON格式的字符串中提取字符串?

[英]ios - how do I extract a string from another string which is in JSON format?

我有一個像這樣的NSString:

[{"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"];

處理完畢后,該根對象應該是數組或字典。 在你的情況下,它顯然是一個包含字典的數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM