如何解析json字符串并将对象存储在iphone中的数组中

时间:2012-04-05 04:47:27

标签: iphone objective-c json

我从服务器获取数据,它响应了一个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"}]  

2 个答案:

答案 0 :(得分: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


}

答案 1 :(得分:0)

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

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

即可
[string_here JSONValue];