NSString即将到来

时间:2014-11-10 07:55:20

标签: ios objective-c json

我是iOS新手。它是我的第一个应用程序,我从服务器调用 JSON 日期。 我必须在tabkeviewcell中存储即将到来的值,但是当我在NSString中设置值时,它将变为零。 以下是我的代码: - .h文件

@interface SecondViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableview;
@property(strong,nonatomic) NSMutableDictionary *jsonresponse;
@property(strong, nonatomic) NSMutableArray *jsonresultarr;

.m文件

- (void)viewDidLoad {
    [self retrievedata];

    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void) retrievedata
{
    //NSMutableArray *jsonresultarr=[NSMutableArray new];

    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://magmasysdev.com/ddc/getAllCompanies.php"]];  // this is your request url

    [request setHTTPMethod:@"GET"];

    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

    NSError *err;
    NSURLResponse *response;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];     // here parsing the array

    NSArray *firstArry=[[jsonArray objectAtIndex:1]objectForKey:@"company_data"];

    for (int i=0; i<[firstArry count]; i++)
    {
        _jsonresponse= [firstArry objectAtIndex:i];

        NSString *Company_Id=[_jsonresponse objectForKey:@"Company_Id"];
        NSString *Company_Name=[_jsonresponse objectForKey:@"Company_Name"];
        NSString *Company_Address=[_jsonresponse objectForKey:@"Company_Address"];
        NSString *Company_Email=[_jsonresponse objectForKey:@"Company_Email"];
        NSString *Company_Tel_Num=[_jsonresponse objectForKey:@"Company_Tel_Num"];
        NSString *Company_Website=[_jsonresponse objectForKey:@"Company_Website"];
        NSString *Company_Fax_Num=[_jsonresponse objectForKey:@"Company_Fax_Num"];
        [_jsonresultarr addObject:Company_Id];
        [_jsonresultarr addObject:Company_Name];
        [_jsonresultarr addObject:Company_Address];
        [_jsonresultarr addObject:Company_Email];
        [_jsonresultarr addObject:Company_Tel_Num];
        [_jsonresultarr addObject:Company_Website];
        [_jsonresultarr addObject:Company_Fax_Num];







    }
     [_tableview reloadData];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(self.jsonresultarr) // coming nil
    { return _jsonresultarr.count;

    }
    return 7;
}

这是我的输出回复。任何人都可以给我一个例子或尝试修改我的代码,以便我可以在文本框中存储数据。

[{"status":"Y"},{"company_data":[{"Company_Id":"5","Company_Name":"SANTE (PVT) LIMITED","Company_Address":"245\/2-Z P.E.C.H.S Block-6 \nKarachi-75400,Pakistan","Company_Email":"sante@sante.com.pk","Company_Tel_Num":"4520507, 4533425","Company_Website":"www.sante.com.pk","Company_Fax_Num":"4.54885e+006"}

3 个答案:

答案 0 :(得分:1)

根据您对数组的响应,您可以在索引“1”处获取关键字“company_data”的字典。

NSDictionary *dict = [[innerObject objectAtIndex:1] objectForKey:@"company_data"];
NSString *strCompanyId = [dict objectForKey:@"Company_Id"];
NSString *strCompanyName = [dict objectForKey:@"Company_Name"];
NSString *strCompanyAddress = [dict objectForKey:@"Company_Address"];
NSString *strCompanyEmail = [dict objectForKey:@"Company_Email"];
NSString *strCompanyPhone = [dict objectForKey:@"Company_Tel_Num"];
NSString *strCompanySite = [dict objectForKey:@"Company_Website"];
NSString *strCompanyFax = [dict objectForKey:@"Company_Fax_Num"];

您可以使用此字符串设置适当的textField!

答案 1 :(得分:0)

-(void) retrievedata
{
    NSURL *url=[NSURL URLWithString:@"http://magmasysdev.com/ddc/getAllCompanies.php"];
    NSData *data=[NSData dataWithContentsOfURL:url];
    jsonarray=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
//    companyarray = [[NSMutableArray alloc]init];
    NSMutableArray *innerObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; // plz check here data is not nil
   NSDictionary *dicarr=[innerObject objectAtIndex:1];
        NSArray *arrCom=[dicarr objectForKey:@"company_data"];
        for (int i=0;i<arrCom.count; i++)
        {
            NSDictionary *dicSub =[arrCom objectAtIndex:i];
            NSString *strId=[dicSub objectForKey:@"Company_Id"];
            NSString *strAddress=[dicSub objectForKey:@"Company_Address"];

        }

}

答案 2 :(得分:0)

 NSMutableArray *jsonresultarr=[NSMutableArray new];   // declare this array in globally

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://magmasysdev.com/ddc/getAllCompanies.php"]];  // this is your request url

[request setHTTPMethod:@"GET"];

[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];     // here parsing the array

NSArray *firstArry=[[jsonArray objectAtIndex:1]objectForKey:@"company_data"];

for (int i=0; i<[firstArry count]; i++)
{
    NSMutableDictionary *getjsonresponse= [firstArry objectAtIndex:i];

    NSString *Company_Id=[getjsonresponse objectForKey:@"Company_Id"];
    NSString *Company_Name=[getjsonresponse objectForKey:@"Company_Name"];
    NSString *Company_Address=[getjsonresponse objectForKey:@"Company_Address"];
    NSString *Company_Email=[getjsonresponse objectForKey:@"Company_Email"];
    NSString *Company_Tel_Num=[getjsonresponse objectForKey:@"Company_Tel_Num"];
    NSString *Company_Website=[getjsonresponse objectForKey:@"Company_Website"];
    NSString *Company_Fax_Num=[getjsonresponse objectForKey:@"Company_Fax_Num"];


}

另一种选择

     for (NSDictionary *tmp in firstArry)
        {
          NSMutableDictionary * itemshowdetails=[[NSMutableDictionary alloc]init];

            [itemshowdetails setValue:[tmp objectForKey:@"Company_Id"] forKey:@"Company_Id"];
            [itemshowdetails setValue:[tmp objectForKey:@"Company_Name"] forKey:@"Company_Name"];
            [itemshowdetails setValue:[tmp objectForKey:@"Company_Address"] forKey:@"Company_Address"];
            [itemshowdetails setValue:[tmp objectForKey:@"Company_Email"] forKey:@"Company_Email"];
            [itemshowdetails setValue:[tmp objectForKey:@"Company_Tel_Num"] forKey:@"Company_Tel_Num"];
            [itemshowdetails setValue:[tmp objectForKey:@"Company_Website"] forKey:@"Company_Website"];

              [itemshowdetails setValue:[tmp objectForKey:@"Company_Fax_Num"] forKey:@"Company_Fax_Num"];


            [jsonresultarr addObject:itemshowdetails];



        }
检索中的

  NSString *pass = [[jsonresultarr objectAtIndex:0] objectForKey:@"Company_Id"];  // here pass your index if already I know else call the line inside the for loop



#pragma mark - tablevie data source

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [jsonresultarr count];

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.01f;
 }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
   .....

   // here pass your details

 cell.Company_Id.text =  [[jsonresultarr objectAtIndex:indexPath.row ] objectForKey:@"Company_Id"]; 

 cell.Company_Name.text =  [[jsonresultarr objectAtIndex:indexPath.row ] objectForKey:@"Company_Name"]; 

 cell.Company_Address.text =  [[jsonresultarr objectAtIndex:indexPath.row ] objectForKey:@"Company_Address"];

  cell. Company_Email.text =  [[jsonresultarr objectAtIndex:indexPath.row ] objectForKey:@"Company_Email"]; 

  cell.Company_Tel_Num.text =  [[jsonresultarr objectAtIndex:indexPath.row ] objectForKey:@"Company_Tel_Num"]; 

 cell.Company_Website.text =  [[jsonresultarr objectAtIndex:indexPath.row ] objectForKey:@"Company_Website"]; 
 cell.Company_Fax_Num.text =  [[jsonresultarr objectAtIndex:indexPath.row ] objectForKey:@"Company_Fax_Num"]; 


return cell;

}