无法在我的控制台中打印我的json数据?

时间:2015-11-02 08:02:36

标签: ios objective-c json core-data

这是我的代码。我使用NSDictionary并编码在我的控制台中打印我的json数据。但我得到这样的错误:

   'NSInvalidArgumentException', reason: '-[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7c971930'

我的代码:

  if(buttonIndex == 0) {
        NSLog(@"OK Button is clicked");
    }
    else if(buttonIndex == 1) {

        if([[textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]!=0)
        {
            if(!self.note)
            {
                NSManagedObjectContext *context = [self managedObjectContext];
                NSManagedObject *newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Notes" inManagedObjectContext:context];

                NSLog(@"%@",textView.text);

                [newNote setValue:textView.text forKey:@"note"];
                if([textView.text length]>30)
                {
                    [newNote setValue:[NSString stringWithFormat:@"%@...",[textView.text substringToIndex:25]] forKey:@"title"];
                }
                else
                    [newNote setValue:textView.text forKey:@"title"];
                [newNote setValue:[NSDate date] forKey:@"mod_time"];
                //[newDevice setValue:self.versionTextField.text forKey:@"version"];
                //[newDevice setValue:self.companyTextField.text forKey:@"company"];

如何解决此问题,并在我的控制台中打印我的数据 帮帮我。我挣扎了2个小时。我用Google搜索并更改所有更改。但是无法在我的控制台中获取数据。提前致谢

3 个答案:

答案 0 :(得分:1)

我猜您可以获得如下所示的数据

NSDictionary *monday = jsonResults[@"original"];
NSArray * arrFile = monday[@"files"];
for (NSDictionary *theCourse in arrFile)
{
    ....
}

答案 1 :(得分:1)

// convert Json to NSDictionary
NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];

// NSLog(@"%@",jsonResults);
int count = [[jsonResults valueForKey:@"count"] intValue];

NSArray *arrayData = [jsonResults copy];

NSMutableArray *arrayPDFName = [[NSMutableArray alloc]init];

for(int i = 0;i < [arrayData count];i++)
{
    NSDictionary *dictOriginal = [[arrayData objectAtIndex:2]valueForKey:@"original"];
    int countOriginal = [[dictOriginal valueForKey:@"count"] intValue];
    NSLog(@"The countOriginal is - %d",countOriginal);
    NSArray *arrayFiles = [[dictOriginal valueForKey:@"files"] copy];
    NSLog(@"The arrayFiles are - %@",arrayFiles);
    for(int j=0;j<[arrayFiles count];j++)
    {
        NSString *strCreatedTime = [NSString stringWithFormat:@"%@",[[arrayFiles objectAtIndex:j] valueForKey:@"created_time"]];
        NSString *strLastModifiedTime = [NSString stringWithFormat:@"%@",[[arrayFiles objectAtIndex:j] valueForKey:@"last_modified_time"]];
        NSString *strID = [NSString stringWithFormat:@"%@",[[arrayFiles objectAtIndex:j] valueForKey:@"id"]];
        NSString *strName = [NSString stringWithFormat:@"%@",[[arrayFiles objectAtIndex:j] valueForKey:@"name"]];

        NSLog(@"The created_time is - %@",strCreatedTime);
        NSLog(@"The last_modified_time is - %@",strLastModifiedTime);
        NSLog(@"The is is - %@",strID);
        NSLog(@"The name is - %@",strName);

        [arrayPDFName addObject:strName];
    }

}

答案 2 :(得分:1)

您是否检查过sendSynchronousRequest中收到的数据(即returnData):是否返回普通数据?

如果收到的数据是在Base64中,您可能必须将此NSData解码为纯数据,然后继续进行字符串转换。

NSData *decodedData = [[NSData alloc] initWithBase64EncodedData:responseData options:NSDataBase64DecodingIgnoreUnknownCharacters];

NSString *str = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
相关问题