1

我设计了一个应用程序,它有一个按钮和三个标签。

当按下按钮时,会调用一个 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 解析器有问题吗?

我可以将所有值、时间、日期存储在一个数组中吗?

并在标签上显示数组中的最后一项?

任何帮助将非常感激。

4

2 回答 2

2

valueForKey当您的json对象是字典条目数组时,似乎普遍存在混淆。Midhun 的回答以一种方式解决了这个问题,即只传递一个字典条目processData(这是一个合乎逻辑的解决方案,因为您的原始代码声明NSString *value明确暗示您希望只传递一个字典条目)。

但是,如果你想为你的字典数组获取所有三个字典键的数组,你会使用你的 original 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 数据有意义的任何日期格式字符串。

于 2012-12-12T14:01:38.233 回答
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]];
于 2012-12-12T13:44:14.470 回答