0

我正在从服务器获取数据,它作为响应提供了一个 NSString,其中包含 json 数据我希望将 json 数据存储在一个数组中如何做到这一点

      NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

       NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];


       NSLog(data);

   NSData* data=[dataString dataUsingEncoding:NSUTF8StringEncoding];



    [self performSelectorOnMainThread:@selector(fetchedData:) 
                           withObject:data waitUntilDone:YES];



     - (void)fetchedData:(NSData *)responseData {
      //parse out the json data
       NSError* error;
       NSDictionary* json = [NSJSONSerialization 
                      JSONObjectWithData:responseData //1

                      options:kNilOptions 
                      error:&error];

     NSArray* latestLoans = [json objectForKey:@"loans"]; //2

       NSLog(@"loans: %@", latestLoans); //3
   }

这是从服务器返回的数据日志

  [{"CodeValue":"90658","CodeDescription":"flu shot","IsActive":"1","CodeType":"CPT","CodeID":"6","UpdateDateTime":"2012-04-02 02:09:46"}]  
4

2 回答 2

0
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];


NSArray *dataArr=[data JSONValue];

for (int i=0; i<[dataArr count]; i++) {

    NSDictionary *dict=[dataArr objectAtIndex:i];

    NSString *codeV=[dict valueForKey:@"CodeValue"];
    NSString *codeD=[dict valueForKey:@"CodeDescription"];
    NSString *active=[dict valueForKey:@"IsActive"];
    NSString *codeT=[dict valueForKey:@"CodeType"];
    NSString *codeId=[dict valueForKey:@"CodeID"];
    NSString *updatedTime=[dict valueForKey:@"UpdateDateTime"];

    NSLog([dict description],nil);
}

    NSLog(@"%@", json_string);


//May this will help you out.

务必将 JSON 库类包含到您的项目中。

我正在以这种方式插入数组,但它返回 o 对象

       NSDictionary *dict=[dataArr objectAtIndex:i];

        SearchCode *theObject =[[SearchCode alloc] init];


        theObject.codeValue=[dict valueForKey:@"CodeValue"];
        theObject.codeDescription=[dict valueForKey:@"CodeDescription"];
        theObject.codeType=[dict valueForKey:@"CodeType"];
        theObject.codeID=[dict valueForKey:@"CodeID"];
        theObject.UpdateDateTime=[dict valueForKey:@"UpdateDateTime"];


        NSLog(codeType);




        [cptArray addObject:theObject];
        [theObject release];
        theObject=nil;



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return [cptArray count];    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    cell.selectionStyle=UITableViewCellSelectionStyleGray;
    cell.accessoryType = UITableViewCellAccessoryNone;

     }
SearchCode *theObject =[cptArray objectAtIndex:indexPath.row];
cell.textLabel.text=theObject.codeValue; //& so on you can access any value from "theObject" here


}
于 2012-04-05T05:07:18.413 回答
0

如果您想要更完整的解决方案,请查看:https ://github.com/stig/json-framework

它是一个很棒的框架,要解析 JSON,您只需键入

[string_here JSONValue];
于 2012-04-05T14:42:08.000 回答