我设计了一个应用程序,它有一个按钮和三个标签。
当按下按钮时,会调用一个 URL。URL 的 JSON 表示形式如下:
[
{
"_id": "50c87e8a1898044ea1458e4c",
"value": "10",
"time": "12:10",
"date": "12.12.12"
},
{
"_id": "50c87f311898044ea1458e4d",
"value": "12",
"time": "13:15",
"date": "12.12.12"
}
]
我的应用程序中还有三个标签,可以显示值、时间和日期。
我的应用程序的代码如下:
-(IBAction)getrequest:(id)sender
{
NSURL *url =[NSURL URLWithString:@"http://localhost:3000/lights"];
NSData* data = [NSData dataWithContentsOfURL:url];
//fetch the data to the JSON Foundation opject.
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
}
- (void)fetchedData:(NSData *)responseData {
NSError *e = nil;
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&e];
NSLog(@"%@", json);
[self processData:json];
}
-(void)processData:(NSDictionary *) JSONObject{
NSString *value = [JSONObject valueForKey:@"value"];
NSString *date = [JSONObject valueForKey:@"date"];
NSString *time = [JSONObject valueForKey:@"time"];
NSLog(@"value: %@", value);
NSLog(@"date: %@", date);
NSLog(@"time: %@", time);
_value.text = value;
_date.text = date;
_time.text = time;
}
代码编译和构建没有任何错误,但是当我按下模拟器中的按钮时,出现如下异常:
2012-12-12 14:16:21.115 Sensor_visulization[7956:11303] value: (
10,
12
)
2012-12-12 14:16:21.116 Sensor_visulization[7956:11303] date: (
"12.12.12",
"12.12.12"
)
2012-12-12 14:16:21.116 Sensor_visulization[7956:11303] time: (
"12:10",
"13:15"
)
2012-12-12 14:16:21.116 Sensor_visulization[7956:11303] -[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x7191a30
2012-12-12 14:16:21.117 Sensor_visulization[7956:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x7191a30'
我的 json 解析器有问题吗?
我可以将所有值、时间、日期存储在一个数组中吗?
并在标签上显示数组中的最后一项?
任何帮助将非常感激。