0
確定這裏是我的本地JSON文件products.json我放在資源文件夾下:如何打印文本字段本地JSON文件的數據
"Products": [
{
"Name": "Samsung LCD",
"Price": "40000",
"Ratings": 4.5,
"Barcode":1
},
{
"Name": "Sony Cybershot",
"Price": "35000",
"Ratings": 3.5,
"Barcode":2
},
{
"Name": "LG Refrigerator",
"Price": "40000",
"Ratings": 4,
"Barcode":3
},
{
"Name":"Toshiba Microwave" ,
"Price": "Hdhd",
"Ratings": 4.5,
"barcode":4
}
]
我有我想要的文本字段打印第一個產品的數據,即點擊一個按鈕,即三星液晶顯示器。
這裏的代碼:(它顯示空輸出)也我需要導入任何文件?
(IBAction爲)enterCode:(ID)發送方{
NSLog(@"HI"); NSString *filePath = [[NSBundle mainBundle] pathForResource:@"products" ofType:@"json"] NSError *error = nil; NSData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error]; id JSONObject = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:&error]; _out1.text = [NSString stringWithFormat: @"%@ %@ %@ %@", JSONObject[@"products"][0][@"Name"], JSONObject[@"products"][0][@"Price"], JSONObject[@"products"][0][@"Ratings"], JSONObject[@"products"][0][@"Barcode"], nil];
}