如何将 JSON 数据添加到 NSARRAY?
How to add the JSON data to NSARRAY?
[{"name":"Mocha","latitude":17.418694 , "longitude":78.445116},
{"name":"Rock Castle","latitude":17.420865 , "longitude":78.442219},
{"name":"RnB Select","latitude":17.420639 , "longitude":78.443635}
]
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
location=locations.lastObject;
[[self latitudeValue] setText:[NSString stringWithFormat:@"%.6f",location.coordinate.latitude]];
[[self longnitudeValue] setText:[NSString stringWithFormat:@"%.6f",location.coordinate.longitude]];
// [[self altitudeValue] setText:[NSString stringWithFormat:@"%.2f feet",location.altitude*METERS_FEET]];
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 2*METERS_MILE, 2*METERS_MILE);
[[self mapView] setRegion:viewRegion animated:YES];
NSArray *arr = [[NSUserDefaults standardUserDefaults] objectForKey:@"myStoredObject"];
NSDictionary *zeroth=arr[0];
NSLog(@"oth position,,,,%@",zeroth);
NSDictionary *firstPosition = arr[1];
NSLog(@"first position is .....%@",firstPosition);
NSDictionary *secondPosition = arr[2];
NSLog(@"second position is .....%@",secondPosition);
MKPointAnnotation *point2 = [[MKPointAnnotation alloc] init];
point2.coordinate =CLLocationCoordinate2DMake([[firstPosition valueForKey:@"latitude"] doubleValue], [[firstPosition valueForKey:@"longitude"] doubleValue]);
point2.title =[firstPosition valueForKey:@"name"];
[self.mapView addAnnotation:point2];
MKPointAnnotation *point3 = [[MKPointAnnotation alloc] init];
point3.coordinate = CLLocationCoordinate2DMake([[firstPosition valueForKey:@"latitude"] doubleValue], [[firstPosition valueForKey:@"longitude"] doubleValue]);
point3.title =[firstPosition valueForKey:@"name"];
[self.mapView addAnnotation:point3];
MKPointAnnotation *point4 = [[MKPointAnnotation alloc] init];
point4.coordinate = CLLocationCoordinate2DMake([[secondPosition valueForKey:@"latitude"] doubleValue], [[secondPosition valueForKey:@"longitude"] doubleValue]);
point4.title =[secondPosition valueForKey:@"name"];
[self.mapView addAnnotation:point4];
MKCircle *circleoverlay = [MKCircle circleWithCenterCoordinate:_mapView.userLocation.coordinate radius:1000];
[circleoverlay setTitle:@"Circle"];
[_mapView addOverlay:circleoverlay];
NSLog(@"circle is drawn");
}
以上代码显示了 Json 数据...
这是我的 JSON 数据,我通过编写以下代码从 URL 获得
NSMutableURLRequest *tRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.ctrlm.in/kvikdata/locations.json"]];
NSLog(@"url is %@",tRequest);
[tRequest setHTTPMethod:@"GET"];
[tRequest setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection
sendAsynchronousRequest:tRequest
queue:queue
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if ([data length] >0 && error == nil){
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error;
id jsonObject = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error];
NSLog(@"Successfully deserialized.2222222..%@",jsonObject);
NSArray *myArr = [json对象复制]; // 此处 myArr = 来自您的响应的您的数据。我不认为它是为了证明
NSLog(@"my array is %@",myArr);
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
[myDefaults setObject:myArr forKey:@"myStoredObject"]; // by this you can store object
if (jsonObject != nil && error == nil){
NSLog(@"Successfully deserialized...%@",jsonObject);
}
});
}
}];
通过写入 NSUserDefaults,第一个数据根据 json 对象进行更新。当我再次将位置名称从 @"mocha" 更改为 @"test1" 时,它不是更新……为什么?
谢谢你
您已经在获取数组,使用 NSArray
代替 id
然后您将在数组
中获得响应
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error:& error];
建议
NSURLConnection
在 ios 9 中已弃用,因此请使用 NSURLSession
这可能对你有帮助
https://www.objc.io/issues/5-ios7/from-nsurlconnection-to-nsurlsession/
https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started
您获取的对象已经是数组。只需使用 NSArray * jsonObject
而不是 id jsonObject
。 Click here for tutorial of tutsplus. Raywenderlich 也是另一个答案中提到的不错的选择。
您应该使用 AFNetworking
来调用网络服务。 AFNetworking - Github
希望这对您有所帮助。 :)
根据评论中的问题更新:
您应该使用本地数据库来存储您从服务器获取的字典或数组。如果数据太多,数据之间的关系复杂,那么可以使用sqlite
或core data
。如果你是简单的数据(如你的问题所示),你可以使用 NSUserDefaults
像下面的例子,
NSArray *myArr = nil; // here myArr = yourData from your response. i take it nil for demonstrate
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
[myDefaults setObject:myArr forKey:@"myStoredObject"]; // by this you can store object
NSArray *arr = [myDefaults objectForKey:@"myStoredObject"]; // like this you can retrivie object
现在您可以使用arr 在地图上显示注释。每当您从服务器调用 [myDefaults setObject:myArr forKey:@"myStoredObject"];
获得响应并且 myArr 应该是新的响应。所以它会用新的覆盖你的旧数据。希望这会有所帮助。 :)
[{"name":"Mocha","latitude":17.418694 , "longitude":78.445116},
{"name":"Rock Castle","latitude":17.420865 , "longitude":78.442219},
{"name":"RnB Select","latitude":17.420639 , "longitude":78.443635}
]
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
location=locations.lastObject;
[[self latitudeValue] setText:[NSString stringWithFormat:@"%.6f",location.coordinate.latitude]];
[[self longnitudeValue] setText:[NSString stringWithFormat:@"%.6f",location.coordinate.longitude]];
// [[self altitudeValue] setText:[NSString stringWithFormat:@"%.2f feet",location.altitude*METERS_FEET]];
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 2*METERS_MILE, 2*METERS_MILE);
[[self mapView] setRegion:viewRegion animated:YES];
NSArray *arr = [[NSUserDefaults standardUserDefaults] objectForKey:@"myStoredObject"];
NSDictionary *zeroth=arr[0];
NSLog(@"oth position,,,,%@",zeroth);
NSDictionary *firstPosition = arr[1];
NSLog(@"first position is .....%@",firstPosition);
NSDictionary *secondPosition = arr[2];
NSLog(@"second position is .....%@",secondPosition);
MKPointAnnotation *point2 = [[MKPointAnnotation alloc] init];
point2.coordinate =CLLocationCoordinate2DMake([[firstPosition valueForKey:@"latitude"] doubleValue], [[firstPosition valueForKey:@"longitude"] doubleValue]);
point2.title =[firstPosition valueForKey:@"name"];
[self.mapView addAnnotation:point2];
MKPointAnnotation *point3 = [[MKPointAnnotation alloc] init];
point3.coordinate = CLLocationCoordinate2DMake([[firstPosition valueForKey:@"latitude"] doubleValue], [[firstPosition valueForKey:@"longitude"] doubleValue]);
point3.title =[firstPosition valueForKey:@"name"];
[self.mapView addAnnotation:point3];
MKPointAnnotation *point4 = [[MKPointAnnotation alloc] init];
point4.coordinate = CLLocationCoordinate2DMake([[secondPosition valueForKey:@"latitude"] doubleValue], [[secondPosition valueForKey:@"longitude"] doubleValue]);
point4.title =[secondPosition valueForKey:@"name"];
[self.mapView addAnnotation:point4];
MKCircle *circleoverlay = [MKCircle circleWithCenterCoordinate:_mapView.userLocation.coordinate radius:1000];
[circleoverlay setTitle:@"Circle"];
[_mapView addOverlay:circleoverlay];
NSLog(@"circle is drawn");
}
以上代码显示了 Json 数据... 这是我的 JSON 数据,我通过编写以下代码从 URL 获得
NSMutableURLRequest *tRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.ctrlm.in/kvikdata/locations.json"]];
NSLog(@"url is %@",tRequest);
[tRequest setHTTPMethod:@"GET"];
[tRequest setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection
sendAsynchronousRequest:tRequest
queue:queue
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if ([data length] >0 && error == nil){
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error;
id jsonObject = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error];
NSLog(@"Successfully deserialized.2222222..%@",jsonObject);
NSArray *myArr = [json对象复制]; // 此处 myArr = 来自您的响应的您的数据。我不认为它是为了证明 NSLog(@"my array is %@",myArr);
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
[myDefaults setObject:myArr forKey:@"myStoredObject"]; // by this you can store object
if (jsonObject != nil && error == nil){
NSLog(@"Successfully deserialized...%@",jsonObject);
}
});
}
}];
通过写入 NSUserDefaults,第一个数据根据 json 对象进行更新。当我再次将位置名称从 @"mocha" 更改为 @"test1" 时,它不是更新……为什么? 谢谢你
您已经在获取数组,使用 NSArray
代替 id
然后您将在数组
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error:& error];
建议
NSURLConnection
在 ios 9 中已弃用,因此请使用 NSURLSession
这可能对你有帮助
https://www.objc.io/issues/5-ios7/from-nsurlconnection-to-nsurlsession/ https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started
您获取的对象已经是数组。只需使用 NSArray * jsonObject
而不是 id jsonObject
。 Click here for tutorial of tutsplus. Raywenderlich 也是另一个答案中提到的不错的选择。
您应该使用 AFNetworking
来调用网络服务。 AFNetworking - Github
希望这对您有所帮助。 :)
根据评论中的问题更新:
您应该使用本地数据库来存储您从服务器获取的字典或数组。如果数据太多,数据之间的关系复杂,那么可以使用sqlite
或core data
。如果你是简单的数据(如你的问题所示),你可以使用 NSUserDefaults
像下面的例子,
NSArray *myArr = nil; // here myArr = yourData from your response. i take it nil for demonstrate
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
[myDefaults setObject:myArr forKey:@"myStoredObject"]; // by this you can store object
NSArray *arr = [myDefaults objectForKey:@"myStoredObject"]; // like this you can retrivie object
现在您可以使用arr 在地图上显示注释。每当您从服务器调用 [myDefaults setObject:myArr forKey:@"myStoredObject"];
获得响应并且 myArr 应该是新的响应。所以它会用新的覆盖你的旧数据。希望这会有所帮助。 :)