Ios 如何使用Objective C从JSON获取特定值并在地图视图上绘图?

Ios 如何使用Objective C从JSON获取特定值并在地图视图上绘图?,ios,objective-c,json,mkmapview,Ios,Objective C,Json,Mkmapview,我需要从下面的JSONresponse中获取特定的值,这些值是我在下面提到的 Reg no: (Need to show callout) Lat : (Need to use drop pin on map) Long : (Need to use drop pin on map) Name : (Need to show callout) Age : (Need to show callout) 注意:从服务器获取的数组值的学校,因此它将根据A、B和C类别增加。它不是静止的 {

我需要从下面的
JSON
response中获取特定的值,这些值是我在下面提到的

Reg no: (Need to show callout)
Lat :  (Need to use drop pin on map)
Long :  (Need to use drop pin on map)
Name : (Need to show callout)
Age : (Need to show callout)
注意:
从服务器获取的数组值的学校,因此它将根据A、B和C类别增加。它不是静止的

{
    A =     {
        school =         (
                        {
               reg_no = 1;
     latitude = "22.345";
                longitude = "-12.4567";

               student =                 (
                                        {
                        name = "akila";
                        age = "23";

                       }
                );
                city = "<null>";
                state = TN;
            },
                                    {
               reg_no = 2;
               latitude = "22.345";
               longitude = "-12.4567";

               student =                 (
                                        {
                        name = "sam";
                        age = "23";

                       }
                );
                city = "<null>";
                state = TN;
            },                                   {
               reg_no = 3;
               latitude = "22.345";
               longitude = "-12.4567";

               student =                 (
                                        {
                        name = "lansi";
                        age = "23";

                       }
                );
                city = "<null>";
                state = TN;
            }
       );
        Schoolname = "Good School";
        categories = school;
   };
}

尝试NSJSONSerialization

NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];

if (!jsonArray) {
  NSLog(@"Error parsing JSON: %@", e);
} else {
   for(NSDictionary *item in jsonArray) {
      NSLog(@"Item: %@", item);
   }
}
您可以使用解析JSON,然后使用Objective-C访问循环中所需的值。完成所有这些后,您可以在
MKMapView
上添加条目作为
MKAnnotation
s,以获得所需内容。请注意,
MKAnnotation
是一个协议,因此需要创建一个实现它的类

MKMapView *mapView = [MKMapView new];
NSDictionary *parsedJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
for (NSDictionary *entry in parsedJSON[@"A"][@"school") {
    NSString *regNo = entry[@"reg_no"];
    NSString *name = entry[@"student"][@"name"];
    NSString *age = entry[@"student"][@"age"];
    double latitude = [entry[@"latitude"] doubleValue];
    double longitude = [entry[@"longitude"] doubleValue];
    id<MKAnnotation> annotation = [/*class implementing MKAnnotation*/ new];
    annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
    annotation.title = name;
    annotation.subtitle = [NSString stringWithFormat:@"Reg: %@, Age: %@", regNo, age];
    [mapView addAnnotation:annotation];
}
MKMapView*mapView=[MKMapView new];
NSDictionary*parsedJSON=[NSJSONSerialization JSONObjectWithData:数据选项:0错误:无];
对于(NSDictionary*parsedJSON[@“A”][@“school”中的条目){
NSString*regNo=entry[@“regu no”];
NSString*name=条目[@“学生”][@“姓名”];
NSString*age=条目[@“学生”][@“年龄”];
双纬度=[输入[@“纬度”]双值];
双经度=[输入[@“经度”]双值];
id annotation=[/*实现MKAnnotation*/new]的类;
annotation.coordinate=CLLocationCoordinate2DMake(纬度、经度);
annotation.title=名称;
annotation.subtitle=[NSString stringWithFormat:@“Reg:%@,Age:%@”,regNo,Age];
[地图视图添加注释:注释];
}
u应该迭代条目[@“student”],以获取每个学生的数据
条目[@“student”][@“name”]将不会获得任何数据,即使条目[@“student”]只有一个

您提供的JSON文件中缺少“response”键。但是,您正在搜索的是
NSDictionary*response=JSON[@“response”]
。这可能是问题所在。除此之外,您可以尝试NSDictionaryvalueForKey:方法在JSON中进行更深入的搜索。

请您提供准确的JSON字符串,这样我们就可以了解您想要的对象格式。这不是有效的JSON字符串。@CrazyDeveloper-现在我已经更新了check now!好的,我会试着让你知道的。谢谢!Btw id annotation=[/*实现MKAnnotation*/new的类];我想在这里实现什么?@Tim JohsensError*错误;NSDictionary*JSON=[NSJSONSerialization JSONObjectWithData:数据选项:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers错误:&错误];NSDictionary*response=JSON[@“response”];NSLog(@“地图绘图响应:%@”,response);FOR(NSDictionary*响应条目[@“A”][@“school”]){这里我没有得到任何值?您得到的错误使我认为您没有使用有效的JSON。您可以使用来查看您的JSON是否有效或其问题所在。您需要创建一个实现
MKAnnotation
协议的类,该类由您决定。
MKMapView *mapView = [MKMapView new];
NSDictionary *parsedJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
for (NSDictionary *entry in parsedJSON[@"A"][@"school") {
    NSString *regNo = entry[@"reg_no"];
    NSString *name = entry[@"student"][@"name"];
    NSString *age = entry[@"student"][@"age"];
    double latitude = [entry[@"latitude"] doubleValue];
    double longitude = [entry[@"longitude"] doubleValue];
    id<MKAnnotation> annotation = [/*class implementing MKAnnotation*/ new];
    annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
    annotation.title = name;
    annotation.subtitle = [NSString stringWithFormat:@"Reg: %@, Age: %@", regNo, age];
    [mapView addAnnotation:annotation];
}