2015-04-22 82 views
-5

將PHP JSON的值變爲NSArray時出現問題。將PHP JSON轉換爲NSArray

這些是我從我的JSON獲得的值:'1451','1450','1449'

如何將其轉換爲NSArray?

+5

歡迎堆棧溢出,你在這裏張貼問題之前,請閱讀[如何提出頁](http://stackoverflow.com/help/how-to-ask) ,發佈提示。 – Quill

回答

1

嘗試NSJSONSerialization並將其分配給id類型的變量,你可以投它就像使用(NSArray)或者你可以從NSJsonserialization

0

方法1

NSString * jstring = @"[1451,1450,1449]"; //your json string 

jstring = [jstring stringByReplacingOccurrencesOfString:@"[" withString:@""]; 
jstring = [jstring stringByReplacingOccurrencesOfString:@"]" withString:@""]; 

NSArray * intArray = [string componentsSeparatedByString:@","];  
//you could create your int from the array like this 
int x = [[intArray objectAtIndex:0]intValue]; 
直接分配到 NSArray類型的變量

Source

方法2

NSObject *o =[NSJSONSerialization JSONObjectWithData:data 
              options:NSJSONReadingMutableContainers 
               error:&error]; 

NSArrary *arr=(NSArray*)o; 

Source