2016-11-29 56 views
0

我有这样如何在类属性中的Objective C中创建json结构?

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

@property (nonatomic, strong) NSDictionary *dictionary; 

@end 

ViewController.m

#import "ViewController.h" 
#import "GoogleMaps.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    //HOW TO ACCESS THE PROPERTY VALUE HERE 
    self.dictionary = @{}; 


    /DUMP ALL FOUND ITEMS 
    for(DummyContainer* geoItem in geoItems) { 
     NSDictionary *item = @{ 
      @"latitude":geoItem.latitude, 
      @"longtitude":geoItem.longtitude 
     }; 

     self.dictionary[geoItem.geoPoint.name] = item; 
    } 

} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

预期的格式的视图控制器类是

var container = { 
    'location':{ 
     'latitude':1233, 
     'longtitude':124 
    } 
} 

这可以通过

let obj = container['location'];

可以访问像这样 obj.latitude;

  • 问题1纬度访问:如何创建一个类属性作为字典和类内部访问?
  • 问题2:如何创建JSON结构并访问值?

我是新来的iOS请提前帮助我。

+0

http://www.appcoda.com/fetch-parse-json-ios-programming-教程/这可以帮助 – PrafulD

+0

你正在混合Objective-C和Swift。我建议只从Swift和Swift开始。 – shallowThought

+0

请帮我找出目标C代码。我正在寻找目标c – Sundar

回答

1

为了创建不可延伸/一成不变Dictionary对象

@property (strong, nonatomic) NSDictionary *myClassDictionary;  

为了创建可扩展/可变Dictionary对象

@property (strong, nonatomic) NSMutableDictionary *myClassMutableDictionary; 

插入所有的值的字典里是这样 您exampleData

'location':{ 
     'latitude':1233, 
     'longtitude':124 
    } 

NSDictionary *dict = @{@"lattitude":@"1233" , @"longitude":@"124"}; 
self.myClassDictionary = @{@"location":dict};//Convert this dictionary into JSON. 

NSError *error; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject: self.myClassDictionary options:NSJSONWritingPrettyPrinted error:&error]; 
NSString jsonString; 
if (! jsonData) { 
    NSLog(@"Got an error: %@", error); 
} else { 
    jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
}