|
|
|
How to programmatically Add multiple <key, value> objects to NSDictionary? |
|
|
|
|
|
|
|
How to programmatically Add multiple <key, value> objects to NSDictionary?
example:
I have this :
NSDictionary = [
"a" : ["fname": "abc", "lname": "def"]
, "b" : ["fname": "ghi", "lname": "jkl"]
, ... : ...
]
But i need
NSDictionary = [
"a" : ["fname": "abc", "lname": "def"],["fname": "abc1", "lname": "def1"]
, "b" : ["fname": "ghi", "lname": "jkl"]
, ... : ...
]
Tag : Mobile, iOS, iPhone, Objective-C
|
|
|
|
|
|
Date :
2014-12-29 18:56:58 |
By :
vishwa |
View :
1051 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Objective-C)
// { "MemberID":"1", "Name":"Weerachai", "Tel":"0819876107" }
NSString *stringData = @"{ \"MemberID\":\"1\", \"Name\":\"Weerachai\", \"Tel\":\"0819876107\" }";
NSData *jsonData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
NSArray *keys = [jsonObjects allKeys];
// value in key name
NSString *strMemberID = [jsonObjects objectForKey:@"MemberID"];
NSString *strName = [jsonObjects objectForKey:@"Name"];
NSString *strTel = [jsonObjects objectForKey:@"Tel"];
NSLog(@"MemberID = %@",strMemberID);
NSLog(@"Name = %@",strName);
NSLog(@"Tel = %@",strTel);
NSLog(@"====================");
// values in foreach loop
for (NSString *key in keys) {
NSLog(@"%@ is %@",key, [jsonObjects objectForKey:key]);
}
iOS/iPhone and JSON (Create JSON and JSON Parsing, Objective-C)
|
|
|
|
|
Date :
2015-01-05 11:44:51 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thanks mr.win. I will try this
|
|
|
|
|
Date :
2015-01-05 15:00:57 |
By :
vishwa |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|