如何从服务器解析json值,并将其显示在tableview单元格中

时间:2017-04-03 07:15:41

标签: ios objective-c iphone json uitableview

  "Names":[  
  {  
     "type":"Name",
     "id":{  
        "type":"Point",
        "coordinates":[  
           80.26483,
           13.08891
        ]
     },
     "properties":{  
       "region":"Tamil Nadu",
        "region_gid":"whosonfirst:region:85672245",
        "county":"Chennai",
        "county_gid":"whosonfirst:county:890505403",
        "label":"Chennai, India"
     }
  },
  {  
     "type":"Name",
     "id":{  
        "type":"Point",
        "coordinates":[  
           80.27847,
           13.08784
        ]
     },




 for(NSDictionary * dict in autocompletedata)
             {
                 Autodata *data = [[Autodata alloc]init];

                 NSMutableArray *coordinates =[dict valueForKey:@"geometry"];
                 NSMutableArray *propertiesarray = [dict valueForKey:@"properties"];


                 for(NSDictionary *properties_dict in propertiesarray)
                 {
                     data.searchterm = [properties_dict objectForKey:@"label"];
                 }
                 NSLog(@"searchterm%@",coordinates);
                  NSLog(@"coordinates%@",propertiesarray);


                 [autodataarray addObject:data];

             }

从服务器解析json值并将其显示在tableview单元格中。

我需要在tableview单元格中显示标签和坐标,区域。

但是,我有一个错误。我不知道如何解析它。我想也许我可以将id和属性保存在不同的数组中,从中我可以得到坐标和标签的值。

3 个答案:

答案 0 :(得分:0)

NSArray *YourArray =[Json objectForKey:@"Names"];

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
NSString *strlabel =[[[YourArray objectAtIndex:indexPath.row]objectForKey:@"properties"]objectForKey:@"label"];

NSString *strregion =[[[YourArray objectAtIndex:indexPath.row]objectForKey:@"properties"]objectForKey:@"region"];

NSArray *cordintateArray =[[[YourArray objectAtIndex:indexPath.row]ojectForKey:@"id"]objectForKey:@"coordinates"];
}

答案 1 :(得分:0)

来自webservice的成功

yourArray=[dictDashboardData objectForKey:@"Names"];

[yourTableView reloadData];

表格方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return yourArray.count;
}


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

//...

NSDictionary *dictData = [yourArray objectAtIndex:indexPath.row];

cell.yourLabel.text=[dictData objectForKey:@"type"];
cell.yourLabel.text=[dictData objectForKey:@"id"];
    cell.yourLabel.text=[[dictData objectForKey:@"id"]objectForKey:@"type"];
cell.yourLabel.text=[[dictData objectForKey:@"id"]objectForKey:@"coordinates"];
cell.yourLabel.text=[dictData objectForKey:@"properties"];

return cell;
}

答案 2 :(得分:0)

self.Array1 = JSON.object(forKey: "Names") as! NSArray
            for i in 0..<self.Array.count {
                let item = self.Array[i] as! [String: AnyObject]
               //declare Object
                let SampleDetails = SampleObjectModel()
                SampleDetails.Name = ApiClients.sharedInstance.nullremove(value:item["type"]as AnyObject) as! String
                                               self.Array2.append(SampleDetails)

            }

// tableview委托

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        // create a new cell if needed or reuse an old one
          let cell = Tableview.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath) as!tableviewcell
       //import objectModel and get the data
        let sampleObject = SampleModel.Array2[indexPath.row]
        cell.Namelabel.text = sampleObject.Name

     }