我是iOS新手,在显示来自以下服务数据的数据时出现问题
[{
"Name": Rahul,
"FatherName": Ravinder,
"Designation": Engineering,
"Profession": Software Eng,
"Height": "5 ft 3 in",
"Weight": "134.5 lbs"
}]
下面是我试过的代码。请帮我找到问题。在此先感谢。
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"];
}
}
答案 0 :(得分: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是类名,因为它是一个类方法,你可以使用实例方法。
答案 1 :(得分:0)
嗨更好的方法是使用AFNetworking进行此类API调用活动 - https://github.com/AFNetworking/AFNetworking
它非常简单,功能更强大。一旦得到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;
}
以上是Model Class。
您的JSON看起来像一个数组。所以你需要迭代它上面的Dictionary值。除此之外,你可以直接传递它。
现在在ViewController类中启动可变数组
并传递响应,如
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;
}
希望这会有所帮助。这是一种强大而有弹性的方法,也是线程安全机制