我设计了一个应用程序,它有一个按钮和三个标签。
按下按钮时,会调用一个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解析器有问题吗?
我可以将所有值,时间,日期存储在数组中吗?
并在标签上显示数组中的最后一项?
任何帮助都会非常感激。
答案 0 :(得分:2)
当valueForKey
对象是一个字典条目数组时,json
的作用似乎存在一般性的混淆。 Midhun的答案,通过只传递一个字典条目到processData
(这是一个逻辑解决方案,因为你的原始代码的NSString *value
声明明确暗示你只希望通过一个字典条目来解决它它)。
但是,如果您想为您的词典数组获取所有三个词典键的数组,则可以使用原始fetchedData
,并对原始valueForKey
对象使用json
返回NSArray
个对象。为此,我建议对原始代码进行一些略微修改:
- (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{
// get the three array structures
NSArray *value = [JSONObject valueForKey:@"value"];
NSArray *date = [JSONObject valueForKey:@"date"];
NSArray *time = [JSONObject valueForKey:@"time"];
// these log statements demonstrate that value, date, and time are arrays
NSLog(@"value: %@", value);
NSLog(@"date: %@", date);
NSLog(@"time: %@", time);
// now, let's grab the last object from each to populate our controls
_value.text = [value lastObject];
_date.text = [date lastObject];
_time.text = [time lastObject];
}
顺便提一下,在您的一条评论中,您提出了一个括号参考,想要绘制这些结果的图表。如果是这种情况,我可能会想到你需要将日期和时间合并到NSDate
个对象中。如果你想要它们作为NSDate
个对象,你可以:
-(void)processData:(NSDictionary *) JSONObject{
// get the values
NSArray *value = [JSONObject valueForKey:@"value"];
// get the datetimes
NSMutableArray *datetimes = [NSMutableArray array];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"dd.MM.yy HH:mm";
formatter.locale = [NSLocale currentLocale];
for (NSDictionary *dictionary in JSONObject)
{
NSString *datetimeString = [NSString stringWithFormat:@"%@ %@", [dictionary objectForKey:@"date"], [dictionary objectForKey:@"time"]];
NSDate *datetime = [formatter dateFromString:datetimeString];
[datetimes addObject:datetime];
}
NSLog(@"values=%@", values);
NSLog(@"datetimes=%@", datetimes);
由于您的示例数据包含今天的唯一日期12/12/12
,因此我无法确定您的日期格式,因此我猜测您正在使用dd.MM.yy
,但显然使用了字符串生成的任何日期格式感谢您的JSON数据。
答案 1 :(得分:1)
json
是NSMutableArray
,您将其作为processData
参数传递给NSDictionary
方法。
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&e];
更改方法调用,如:
[self processData:[json objectAtIndex:0]];
以上将显示第一个元素。如果您需要显示最后一个元素,请使用:
[self processData:[json objectAtIndex:[json count]-1]];