【问题标题】:Populating class from json data从 json 数据填充类
【发布时间】:2013-11-13 12:50:57
【问题描述】:

我有一个 WCF,它以以下格式返回一些 json 数据:

{
"RetrieveLocationsResult": [
   {
        "Address": "106 Mullaghboy Road",
        "Category": "Tarmac",
        "Closest_Property_Number": 106,
        "ID": 33,
        "Image1": 1234,
        "Image2": 0,
        "Image3": 0,
        "Latitude": 5,
        "Longitude": "-1.541902",
        "Notes": "New Driveway",
        "User_ID": 1
    },
    {
        "Address": "3 drumard road",
        "Category": "Tarmac",
        "Closest_Property_Number": 3,
        "ID": 40,
        "Image1": 23421,
        "Image2": 0,
        "Image3": 0,
        "Latitude": 4,
        "Longitude": "-2.541902",
        "Notes": "new gates",
        "User_ID": 1
    },
]
}

这里可以看到完整的http://crm.fpmccann.co.uk/TemperatureWebService/iphonewebservice.svc/retrievelocations

我有一个名为 location 的类,其中包含每个变量。我想要做的是将这个 json 数据解析到每个位置,有点像制作一个位置数组,这些位置将使用经度和纬度放置在地图上。

目前我解析数据的方法是:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

     NSString *urlAsString = @"http://crm.fpmccann.co.uk/TemperatureWebService/iphonewebservice.svc/retrievelocations";
     NSURL *url = [NSURL URLWithString:urlAsString];
     NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

     [NSURLConnection
      sendAsynchronousRequest:urlRequest
      queue:[[NSOperationQueue alloc] init]
      completionHandler:^(NSURLResponse *response,
                          NSData *data,
                          NSError *error)

      {
          NSLog(@"error: %@", error);
          NSLog(@"data: %@", data);
          NSLog(@"response: %@", response);

          if ([data length] >0 && error == nil)
          {
              NSArray *array = [NSJSONSerialization JSONObjectWithData:data
                                                               options:0
                                                                 error:&error];
              NSLog(@"Response: %@", array);

              CLLocationCoordinate2D location;                         // coordinates of the annotation
              NSMutableArray *newAnnotations = [NSMutableArray array]; // an array in which we'll save our annotations temporarily
              MKPointAnnotation *newAnnotation;                        // the pointer to the annotation we're adding

              NSMutableArray *location = [array valueForKey:@"RetrieveLocationsResult"];


              NSMutableArray *longitude = [[array valueForKey:@"RetrieveLocationsResult"] valueForKey:@"Longitude"];


             // NSLog(@"Response", response, data);
          }
          else if ([data length] == 0 && error == nil)
          {
              NSLog(@"Nothing was downloaded.");
          }
          else if (error != nil){
              NSLog(@"Error = %@", error);
          }

      }];


}

请原谅这是在视图中加载的,它会在它工作后立即更改。 它正在创建和称为位置的数组,其中有 4 个对象,但其中没有任何内容。我想要做的是创建一个 for 循环并根据结果填充一个类?

【问题讨论】:

  • 为您的位置类创建一个initWIthDictionary:(NSDictionary*)dict 方法并让它读取字典并设置对象字段。如果值都很简单并且字段名称与字典键匹配,则可以使用“键/值编码”在一定程度上“自动化”。

标签: objective-c arrays json class sendasynchronousrequest


【解决方案1】:

为检索到的结果创建 JSON 字典:

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSMutableArray *retrieveLocationsResultArr = [NSMutableArray arrayWithArray:[jsonDict objectForKey:@"RetrieveLocationsResult"]];

[retrieveLocationsResultArr enumerateObjectsUsingBlock:^(NSDictionary *locationDict, NSUInteger idx, BOOL *stop) {
    //Create your Location Objects
    NSString *addressStr = [locationDict valueForKey:@"Address"];//sample, extract all values you want in your custom objects.
}];

【讨论】:

  • 您能否向我解释一下这部分代码,谢谢 [retrieveLocationsResultArr enumerateObjectsUsingBlock:^(NSDictionary *locationDict, NSUInteger idx, BOOL *stop) { //创建您的位置对象NSString *addressStr = [locationDict valueForKey:@"Address"];//示例,提取自定义对象中所需的所有值。 }];
猜你喜欢
  • 1970-01-01
  • 2017-05-07
  • 2017-02-03
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多