2012-06-02 25 views
17

का उपयोग कर स्थानीय फ़ाइल से डेटा पास करना मैं अपने JSON फ़ाइल से लेबल्स को एक साधारण व्यू कंट्रोलर पर डेटा पास करने का प्रयास कर रहा हूं लेकिन मुझे नहीं पता कि वास्तव में उस डेटा को कहां पास करना है। क्या मैं बस अपनी setDataToJson विधि में जोड़ सकता हूं या क्या मैं अपने viewDidLoad विधि में डेटा जोड़ूंगा?जेसन

यहाँ मेरी कोड

@interface NSDictionary(JSONCategories) 
+(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation; 
@end 

@implementation NSDictionary(JSONCategories) 

+(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation{ 
    NSData* data = [NSData dataWithContentsOfFile:fileLocation]; 
    __autoreleasing NSError* error = nil; 
    id result = [NSJSONSerialization JSONObjectWithData:data 
               options:kNilOptions error:&error]; 
    if (error != nil) return nil; 
    return result; 
} 
@end 

@implementation ViewController 
@synthesize name; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

} 

-(void)setDataToJson{ 

    NSDictionary *infomation = [NSDictionary dictionaryWithContentsOfJSONString:@"Test.json"]; 
    name.text = [infomation objectForKey:@"AnimalName"];//does not pass data 
} 

उत्तर

40

समस्या तरह से आप अपने फ़ाइल को प्राप्त करने की कोशिश कर रहे है। इसे सही करने के लिए, आपको बंडल में अपना रास्ता पहले खोजना चाहिए। एक बार अपने दृश्य लोड किया जाता है, तो आप इस तरह json पुन: प्राप्त करने से अपने लेबल सेट करने के लिए सक्षम होना चाहिए

+(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation{ 
    NSString *filePath = [[NSBundle mainBundle] pathForResource:[fileLocation stringByDeletingPathExtension] ofType:[fileLocation pathExtension]]; 
    NSData* data = [NSData dataWithContentsOfFile:filePath]; 
    __autoreleasing NSError* error = nil; 
    id result = [NSJSONSerialization JSONObjectWithData:data 
               options:kNilOptions error:&error]; 
    // Be careful here. You add this as a category to NSDictionary 
    // but you get an id back, which means that result 
    // might be an NSArray as well! 
    if (error != nil) return nil; 
    return result; 
} 

कि करने के बाद और: कुछ इस तरह का प्रयास करें

-(void)setDataToJson{ 
    NSDictionary *infomation = [NSDictionary dictionaryWithContentsOfJSONString:@"Test.json"]; 
    self.name.text = [infomation objectForKey:@"AnimalName"]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self setDataToJson]; 
} 
+0

आह यह पूरी तरह से काम करता है !! बहुत बहुत धन्यवाद :) – domshyra

+0

यदि मैं जेसन फ़ाइल 'self.lat.text = [[सूचना मूल्यफोरकी: @ "स्थान"] मूल्य में डेटा की एक सरणी से जानकारी प्राप्त करना चाहता हूं तो मैं अपना nsdictionary फ़ंक्शन बदलूंगा: valueForKey: @ " अक्षांश "];' यह है कि मैं क्या है और इस JSON की तरह लग रहा है 'क्या है" स्थान ": [ { " देशांतर ": 0, " अक्षांश ": 0 } ],' और जब मैं इसे चलाएं यह कहता है '- [__ NSArrayI isEqualToString:]: अज्ञात चयनकर्ता उदाहरण के लिए भेजा गया – domshyra

+0

फिर आपको ऐसा कुछ करना चाहिए:' self.lat.text = [[[सूचना मानफोरकी: @ "स्थान"] ऑब्जेक्टएट इंडेक्स: 0] valueForKey : @ "अक्षांश"]; ' – Alladinian

1

यह valueForKey बजाय होना चाहिए।

उदाहरण:

name.text = [infomation valueForKey:@"AnimalName"];