0

jsonファイルに英語の文字があると、読み取ったjsonが配列に出力されますが、チベット文字(utf8)があるとnullが出力されます。私はjsonとxcodeコードを貼り付けています。助けてください。このコードは、コアデータチュートリアルから試しているものです

        int main(int argc, const char * argv[])
    {

        @autoreleasepool {
            // Create the managed object context
            NSManagedObjectContext *context = managedObjectContext();

            // Custom code here...
            // Save the managed object context
            NSError *error = nil;
            if (![context save:&error]) {
                NSLog(@"Error while saving %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
                exit(1);
            }
            NSError* err = nil;
            NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"tsikzoe" ofType:@"json"];

            NSDictionary* tsikzoe = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]options:kNilOptions error:&err];
 NSLog(@"Imported tsikzoe: %@",[tsikzoe objectForKey:@"wordDef"]);


        }
        return 0;
    }

これがJSONです

[{"id":1,"dictionaryType":1,"word":"༼༡༠༠༦༽ སྲོང་བཙན་ཆོས་ཀྱི་རྗེ་བོ་ཡབ་ཡུམ་གསུམ།","wordDef":"རྒྱལ་པོ་སྲུང་བཙན་སྒམ་པོ། བལ་བཟའ་ཁྲི་བཙུན། རྒྱ་བཟའ་ཀོང་ཇོ་བཅས་གསུམ་ལ་ཟེར།","dateSubmitted":"2012-08-19 00:00:00","author":2,"authorName":"དུང་དཀར་ཚིག་མཛོད།"},
  {"id":2,"dictionaryType":1,"word":"༼༡༠༤༠༽ བསོ།","wordDef":"བསོ་བསོ་ཞེས་པའི་སྒྲའི་ཁྱད་པར་གྱི་མིང༌།","dateSubmitted":"2012-08-19 00:00:00","author":2,"authorName":"དུང་དཀར་ཚིག་མཛོད།"}]
4

1 に答える 1

0
 NSDictionary* tsikzoe = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]options:kNilOptions error:&err];

この場合、JASONは配列であるため、この配列の2つの要素のいずれかにアクセスする必要があります。これは辞書になります。

辞書JSONはこの形式になります

{
"employees": [    //right here! 
{ "firstName":"John" , "lastName":"Doe" }, 
{ "firstName":"Anna" , "lastName":"Smith" }, 
{ "firstName":"Peter" , "lastName":"Jones" }
]
}

配列JSONはこの形式になります

{
[    //notice the difference. 
{ "firstName":"John" , "lastName":"Doe" }, 
{ "firstName":"Anna" , "lastName":"Smith" }, 
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
于 2012-11-28T07:25:19.607 に答える