我需要从JSON
以下的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)
响应中获取特定值,这些值是我在下面提到的
NOTE :
{
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;
};
}
从服务器获取的数组值学校因此它将根据A,B和C类别增加。它不是静止的!
if (data) {
NSError *error;
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:&error];
NSDictionary *response = JSON[@"response"];
for (NSDictionary *entry in response[@"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];
MKPointAnnotation *myAnnotation = [[MKPointAnnotation alloc] init];
myAnnotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
myAnnotation.title = name;
myAnnotation.subtitle = [NSString stringWithFormat:@"Reg: %@, Age: %@", regNo, age];
[mapView addAnnotation:myAnnotation];
}
我的代码(代码不起作用):
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="@dimen/custom_tab_layout_height"
android:background="@color/tab_background_primary"
app:tabGravity="fill"
app:tabIndicatorColor="@color/primary_white"
app:tabIndicatorHeight="3dp"
app:tabMinWidth="120dp"
app:tabMode="scrollable"
app:tabPaddingStart="-1dp"
app:tabPaddingEnd="-1dp"
app:tabPaddingTop="1dp"
app:tabPaddingBottom="1dp"
/>
答案 0 :(得分:0)
尝试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);
}
}
答案 1 :(得分:0)
您可以使用NSJSONSerialization
来解析JSON,然后使用Objective-C subscripting syntax可以在循环中访问所需的值。完成所有这些操作后,您可以在MKAnnotation
上将条目添加为MKMapView
以获得所需内容。请注意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];
}
答案 2 :(得分:0)
你应该迭代参赛作品[@“student”]来获取每个学生的数据 即使条目[@“student”]只有一个
,[@“student”] [@“name”]也不会获得数据答案 3 :(得分:0)
您提供的JSON文件缺少响应&#39;关键在于它。但是,您搜索的是NSDictionary *response = JSON[@"response"];
。这可能是问题所在。除此之外,您可以尝试使用 NSDictionary 的 valueForKey:方法深入搜索您的JSON。