am new to iOS, Getting issue with displaying data from below service data
我是iOS新手,在显示来自以下服务数据的数据时出现问题
[{
"Name": Rahul,
"FatherName": Ravinder,
"Designation": Engineering,
"Profession": Software Eng,
"Height": "5 ft 3 in",
"Weight": "134.5 lbs"
}]
below is the code what i have tried. Please help me to find the issue. Thanks In Advance.
下面是我试过的代码。请帮我找到问题。提前致谢。
NameDetails.m
---------------
- (void)viewDidLoad {
[super viewDidLoad];
[self callService:[appDelegate.signUpdata objectForKey:@"id"]];
}
-(void)callService:(NSString *)userid
{
[Utility showIndicator:nil view1:self.view];
JsonServicePostData = [[JsonServiceCls alloc] init];
JsonServicePostData.delegate = self;
[JsonServicePostData Getdata:userid];
}
-(void)DidFinishWebServicesPostData
{
[Utility hideIndicator];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
_txtName.text=[dict objectForKey:@"Name"];
_txtFName.text=[dict objectForKey:@"FatherName"];
_txtDesg.text=[dict objectForKey:@"Designation"];
_txtprof.text=[dict objectForKey:@"Profession"];
_txtHeight.text=[dict objectForKey:@"Height"];
_txtWeight.text=[dict objectForKey:@"Weight"];
}
}
2 个解决方案
#1
1
+(void)makeHttpGETresponceParsingwithSerVer:(NSString *)strServer withCallBack:(void(^)(NSDictionary *dicArr,NSError *error))handler
{
NSURL *urlServer = [NSURL URLWithString:strServer];
NSURLRequest *request = [NSURLRequest requestWithURL:urlServer];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
handler(res,error);
}];
[postDataTask resume];
}
then call your method In viewDidLoad...
[RestClient makeHttpGETresponceParsingwithSerVer:@"YOUR_URL" withCallBack:^(NSDictionary *responceDic, NSError *error) {
_txtName.text =[responceDic objectForKey:@"Name"];
_txtFName.text =[responceDic objectForKey:@"FatherName"];
_txtDesg.text =[responceDic objectForKey:@"Designation"];
_txtprof.text =[responceDic objectForKey:@"Profession"];
_txtHeight.text =[responceDic objectForKey:@"Height"];
_txtWeight.text =[responceDic objectForKey:@"Weight"];
}];
// RestClient is the class name as it is a class method, You can use instance method.
// RestClient是类名,因为它是一个类方法,您可以使用实例方法。
#2
0
Hi The better approach is for this kind of API call activity you have to go with AFNetworking - https://github.com/AFNetworking/AFNetworking
嗨更好的方法是使用AFNetworking进行这种API调用活动 - https://github.com/AFNetworking/AFNetworking
Its Pretty simple and more powerful. Once you get the json response you have to go for Model Approach.
它非常简单,功能更强大。一旦得到json响应,就必须选择Model Approach。
#import <UIKit/UIKit.h>
@interface NameDetails : NSObject
@property (nonatomic, strong) NSString * designation;
@property (nonatomic, strong) NSString * fatherName;
@property (nonatomic, strong) NSString * height;
@property (nonatomic, strong) NSString * name;
@property (nonatomic, strong) NSString * profession;
@property (nonatomic, strong) NSString * weight;
-(instancetype)initWithDictionary:(NSDictionary *)dictionary;
-(NSDictionary *)toDictionary;
@end
#import "RootClass.h"
NSString *const kRootClassDesignation = @"Designation";
NSString *const kRootClassFatherName = @"FatherName";
NSString *const kRootClassHeight = @"Height";
NSString *const kRootClassName = @"Name";
NSString *const kRootClassProfession = @"Profession";
NSString *const kRootClassWeight = @"Weight";
@interface RootClass ()
@end
@implementation RootClass
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
self = [super init];
if(![dictionary[kRootClassDesignation] isKindOfClass:[NSNull class]]){
self.designation = dictionary[kRootClassDesignation];
}
if(![dictionary[kRootClassFatherName] isKindOfClass:[NSNull class]]){
self.fatherName = dictionary[kRootClassFatherName];
}
if(![dictionary[kRootClassHeight] isKindOfClass:[NSNull class]]){
self.height = dictionary[kRootClassHeight];
}
if(![dictionary[kRootClassName] isKindOfClass:[NSNull class]]){
self.name = dictionary[kRootClassName];
}
if(![dictionary[kRootClassProfession] isKindOfClass:[NSNull class]]){
self.profession = dictionary[kRootClassProfession];
}
if(![dictionary[kRootClassWeight] isKindOfClass:[NSNull class]]){
self.weight = dictionary[kRootClassWeight];
}
return self;
}
/**
* Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property
*/
-(NSDictionary *)toDictionary
{
NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
if(self.designation != nil){
dictionary[kRootClassDesignation] = self.designation;
}
if(self.fatherName != nil){
dictionary[kRootClassFatherName] = self.fatherName;
}
if(self.height != nil){
dictionary[kRootClassHeight] = self.height;
}
if(self.name != nil){
dictionary[kRootClassName] = self.name;
}
if(self.profession != nil){
dictionary[kRootClassProfession] = self.profession;
}
if(self.weight != nil){
dictionary[kRootClassWeight] = self.weight;
}
return dictionary;
}
The above one is Model Class.
以上是模型类。
Your JSON look like a array. So you need to iterate the Dictionary values on it. Other than that you may pass it directly.
您的JSON看起来像一个数组。所以你需要迭代它上面的Dictionary值。除此之外,您可以直接传递它。
Now in your ViewController class initiate the mutable array
现在在ViewController类中启动可变数组
and pass the response like
并传递响应
NSArray *arrayData = ResponseFromAFNETWORKING
for (NSDictionary *data in arrayData) {
NameDetails *modelFeed = [[NameDetails alloc] initFromDictinary:data]
[self.YourMutableDictionary addObject:modelFeed]
}
self.updateDisplay:self.YourMutableDictionary[0] // If not array No iteration, you can prepare the model and pass it directly
----------------------------------------
- (void)updateDisplay:(NameDetails *)feed {
_txtName.text =feed.Name;
_txtFName.text =feed.FatherName;
_txtDesg.text =feed.Designation;
_txtprof.text =feed.Profession;
_txtHeight.text =feed.Height;
_txtWeight.text =feed.Weight;
}
Hope this will help. This is a robust and elastic approach, thread safe mechanism too
希望这会有所帮助。这是一种强大而有弹性的方法,也是线程安全机制
#1
1
+(void)makeHttpGETresponceParsingwithSerVer:(NSString *)strServer withCallBack:(void(^)(NSDictionary *dicArr,NSError *error))handler
{
NSURL *urlServer = [NSURL URLWithString:strServer];
NSURLRequest *request = [NSURLRequest requestWithURL:urlServer];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
handler(res,error);
}];
[postDataTask resume];
}
then call your method In viewDidLoad...
[RestClient makeHttpGETresponceParsingwithSerVer:@"YOUR_URL" withCallBack:^(NSDictionary *responceDic, NSError *error) {
_txtName.text =[responceDic objectForKey:@"Name"];
_txtFName.text =[responceDic objectForKey:@"FatherName"];
_txtDesg.text =[responceDic objectForKey:@"Designation"];
_txtprof.text =[responceDic objectForKey:@"Profession"];
_txtHeight.text =[responceDic objectForKey:@"Height"];
_txtWeight.text =[responceDic objectForKey:@"Weight"];
}];
// RestClient is the class name as it is a class method, You can use instance method.
// RestClient是类名,因为它是一个类方法,您可以使用实例方法。
#2
0
Hi The better approach is for this kind of API call activity you have to go with AFNetworking - https://github.com/AFNetworking/AFNetworking
嗨更好的方法是使用AFNetworking进行这种API调用活动 - https://github.com/AFNetworking/AFNetworking
Its Pretty simple and more powerful. Once you get the json response you have to go for Model Approach.
它非常简单,功能更强大。一旦得到json响应,就必须选择Model Approach。
#import <UIKit/UIKit.h>
@interface NameDetails : NSObject
@property (nonatomic, strong) NSString * designation;
@property (nonatomic, strong) NSString * fatherName;
@property (nonatomic, strong) NSString * height;
@property (nonatomic, strong) NSString * name;
@property (nonatomic, strong) NSString * profession;
@property (nonatomic, strong) NSString * weight;
-(instancetype)initWithDictionary:(NSDictionary *)dictionary;
-(NSDictionary *)toDictionary;
@end
#import "RootClass.h"
NSString *const kRootClassDesignation = @"Designation";
NSString *const kRootClassFatherName = @"FatherName";
NSString *const kRootClassHeight = @"Height";
NSString *const kRootClassName = @"Name";
NSString *const kRootClassProfession = @"Profession";
NSString *const kRootClassWeight = @"Weight";
@interface RootClass ()
@end
@implementation RootClass
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
self = [super init];
if(![dictionary[kRootClassDesignation] isKindOfClass:[NSNull class]]){
self.designation = dictionary[kRootClassDesignation];
}
if(![dictionary[kRootClassFatherName] isKindOfClass:[NSNull class]]){
self.fatherName = dictionary[kRootClassFatherName];
}
if(![dictionary[kRootClassHeight] isKindOfClass:[NSNull class]]){
self.height = dictionary[kRootClassHeight];
}
if(![dictionary[kRootClassName] isKindOfClass:[NSNull class]]){
self.name = dictionary[kRootClassName];
}
if(![dictionary[kRootClassProfession] isKindOfClass:[NSNull class]]){
self.profession = dictionary[kRootClassProfession];
}
if(![dictionary[kRootClassWeight] isKindOfClass:[NSNull class]]){
self.weight = dictionary[kRootClassWeight];
}
return self;
}
/**
* Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property
*/
-(NSDictionary *)toDictionary
{
NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
if(self.designation != nil){
dictionary[kRootClassDesignation] = self.designation;
}
if(self.fatherName != nil){
dictionary[kRootClassFatherName] = self.fatherName;
}
if(self.height != nil){
dictionary[kRootClassHeight] = self.height;
}
if(self.name != nil){
dictionary[kRootClassName] = self.name;
}
if(self.profession != nil){
dictionary[kRootClassProfession] = self.profession;
}
if(self.weight != nil){
dictionary[kRootClassWeight] = self.weight;
}
return dictionary;
}
The above one is Model Class.
以上是模型类。
Your JSON look like a array. So you need to iterate the Dictionary values on it. Other than that you may pass it directly.
您的JSON看起来像一个数组。所以你需要迭代它上面的Dictionary值。除此之外,您可以直接传递它。
Now in your ViewController class initiate the mutable array
现在在ViewController类中启动可变数组
and pass the response like
并传递响应
NSArray *arrayData = ResponseFromAFNETWORKING
for (NSDictionary *data in arrayData) {
NameDetails *modelFeed = [[NameDetails alloc] initFromDictinary:data]
[self.YourMutableDictionary addObject:modelFeed]
}
self.updateDisplay:self.YourMutableDictionary[0] // If not array No iteration, you can prepare the model and pass it directly
----------------------------------------
- (void)updateDisplay:(NameDetails *)feed {
_txtName.text =feed.Name;
_txtFName.text =feed.FatherName;
_txtDesg.text =feed.Designation;
_txtprof.text =feed.Profession;
_txtHeight.text =feed.Height;
_txtWeight.text =feed.Weight;
}
Hope this will help. This is a robust and elastic approach, thread safe mechanism too
希望这会有所帮助。这是一种强大而有弹性的方法,也是线程安全机制