I'm horrible at JSON. I don't understand a single thing. My JSON response looks like this:
{
ID = 1;
EDate = "<null>";
SelectedDay = "/Date(-62135596800000)/";
End = "14.09.2013 15:00:00";
Start = "14.09.2013 07:00:00";
SDate = "<null>";
},
{
ID = 1;
EDate = "<null>";
SelectedDay = "/Date(-62135596800000)/";
End = "14.09.2013 16:00:00";
Start = "14.09.2013 07:00:00";
SDate = "<null>";
},
In both NSData and NSDictionary. How can I loop trough, for example, the "End" property of each object, and add them to an array?
Edit:
I log from this code:
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:result.data options:kNilOptions error:&error];
NSLog(#"Response: %#",dict);
and the complete log is:
This JSON seems to be an array of dictionaries. Try with:
NSMutableArray *endValuesArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in JSONArray) {
[endValuesArray addObject:[dictionary valueForKey:#"End"]];
}
Where JSONArray is the array obtained after NSJSONSerialization.
If you really just need an array of the values for a single key in each dictionary then you can use KVC:
NSArray *endValues = [resultsArray valueForKey:#"End"];
-- This is assuming that you do have an array of dictionaries and that your pasted log just doesn't show the full story.
If you need multiple keys / values out of the dictionaries then you're best to iterate over the contents and pick each item. There are various methods of iteration that you can look at using plain loops or blocks.
Use this :
NSMutableArray *endDatesArray = [NSMutableArray new]; // Here this array will store all end dates
for (int i =0; i < [YOUR_JSONARRAY count]; i++) // Here YOUR_JSONARRAY is the response array you are getting
{
NSMutableDictionary *dict= [YOUR_JSONARRAY objectAtIndex:i];
[endDatesArray addObject:[dict objectForKey:#"End"]];
}
Hope it helps you.
Make an NSArray of the JSON Object.
Use a FOR loop up to the count of the array to create an NSDictionary for each array object
Use 'objectForKey:#"End"' to extract the End object. (within the for loop)
Related
I have a NSMutableDictionary to which I want to change the value of an element.
//My dictionary:
{
objectId = 8ED998yWd1;
cardInfo = {
state = published; //THIS!
price = 40;
color = red;
}
};
I have tried several ways, but the value does not change, like this:
[dictionary setObject:#"reserved" forKey:#"state"]; //nope
or this:
[dictionary setValue:#"reserved" forKeyPath:#"cardInfo.state"]; //nope
or that:
[[dictionary objectForKey:#"cardInfo"] setObject:#"reserved" forKey:#"state"]; //no
and this:
[dictionary setObject:#"reserved" forKey:[[dictionary objectForKey:#"cardInfo"] objectForKey:#"state"]];
How can I change the object "state" from "published" to "reserved"?
Thank you!
Assuming that both dictionary and cardInfo are NSDictionary instances:
You could get a mutable copy of the nested dictionary, modify the appropriate value, and write the modified dictionary back into the "top level" dictionary, like so:
NSMutableDictionary *mutableDict = [dictionary mutableCopy];
NSMutableDictionary *innerDict = [dictionary[#"cardInfo"] mutableCopy];
innerDict[#"state"] = #"reserved";
mutableDict[#"cardInfo"] = innerDict;
dictionary = [mutableDict copy];
I guess you could squeeze that in one line, but it would be one ugly line.
Edit:
If both the outer and inner dictionary were already mutable that would simplify things a bit, of course:
NSMutableDictionary *innerDict = dictionary[#"cardInfo"];
innerDict[#"state"] = #"reserved";
dictionary[#"cardInfo"] = innerDict;
I have NSArray of NSDictionaries which is coming from server as JSON format. I want to add one more NSDictinoary to the NSarray of NSdictionaries. Nsdictionary is based on key value pair. This is the response which I am getting. I want to add another dictionary of same format into "table". Pls Help..
Thanks in advance..!!
Table = (
{
Name = “xyz”;
Recordid = 3;
prefrenceorder = 1;
},
{
Name = “ABC”;
Recordid = 2;
prefrenceorder = 2;
},
{
Name = “swe”;
Recordid = 450;
prefrenceorder = 3;
},
{
Name = “asd”;
Recordid = 451;
prefrenceorder = 4;
}
);
As the NSArray coming from server is immutable so you need to create a new instance of NSMutableArray to add your own dictionary with the response coming from server.
NSMutableArray *newTable = [NSMutableArray arrayWithArray:Table];
[newTable addObject:your_new_dictionary];
Put the NSDictionary's into a NSMutableArray. And then from your instance add your new object NSDictionary
i.e
//The array which has your NSDictionary objects
NSArray *array;
//Putting this array into mutable one in order to add and delete elements
NSMutableArray *mArray = [array mutableCopy];
//Your custom object that you want to add
NSDictionary *yourObject;
//Add it to the array and voila
[mArray addObject:yourObject];
It is better not to convert immutable array to a mutable but to use appropriate NSJSONSerialization option - NSJSONReadingMutableContainers
Specifies that arrays and dictionaries are created as mutable objects.
NSMutableArray *mutableArray = [NSJSONSerialization JSONObjectWithData:yourData options:NSJSONReadingMutableContainers error:nil];
[mutableArray addObject:yourNewDictionary];
How to convert an array into a dictionary in Objective-C language?
This is the array:
[{"1":"2"}, {"2":"3"}]
But I want :
{"1":"2", "2":"3"}
Please help me, I am a new iPhone developer.
I am assuming that you want to combine an array of dictionaries into a single dictionary.
NSArray *array = #[#{#"1":#"2"},#{#"2":#"3"}];
NSMutableDictionary *result = [[NSMutableDictionary alloc]init];
for (NSDictionary *dict in array)
{
[result addEntriesFromDictionary:dict];
}
/*
result = {
1 = 2;
2 = 3;
}
*/
Where did your data come from? The easiest way to "convert" the above is to simply create a single dictionary in the first place.
Otherwise:
NSMutableDictionary* newDict = [NSMutableDictionary dictionary];
for (NSDictionary* oldDict in sourceArray) {
[newDict addEntriesFromDictionary:oldDict];
}
I have a NSArray With NSMutableDictionary object inside this array like the following .What i want to get an object say at index:2 of this array and add that NSMutableDictionary object from the following array into other NSMutableDictionary already have say five elements at positions 6:
(
{
eventId = 2;
eventName = "Sweels ";
},
{
eventId = 1;
eventName = "Lakenge";
},
{
eventId = 23;
eventName = "Royta";
}
)
I am able to get the NSMutableDictionary object and and can extract the key and value and then can add but want to add whole object without extracting the key and values
{
eventId = 23;
eventName = "Royta";
}
You can initialize an NSMutableDictionary object with the keys and values contained in another given dictionary using this initializer:
- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary
Example:
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] initWithDictionary:extractedDict];
If You do not want to create a new NSMutableDictionary Object and just need to add Key/Values to an existing Object then Use this Method:
- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues
Example:
[newDict setValuesForKeysWithDictionary:extractedDict];
If you want all the values as single object than you must do this-
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObjectsFromArray:[paramsDictionary allValues]];
NSLog(#"%#",[paramsDictionary allValues]);
NSMutableDictionary *expense_ArrContents = [[NSMutableDictionary alloc]init];
for (int i = 1; i<=4; i++) {
NSMutableArray *current_row = [NSMutableArray arrayWithObjects:#"payer_id",#"Expense_Type_id",#"Category_Id",#"SubCategory_Id",nil];
[expense_ArrContents setObject:current_row forKey: [NSNumber numberWithInt:i]];
}
NSArray *newArray = [expense_ArrContents allKeysForObject:#"payer_id"];
NSLog(#"%#",[newArray description]);
i want to get the list of key values containing the particular object which is in the array of values stored in nsmutabledictionary for a particular key.
In the line where you get all the keys ([expense_ArrContents allKeysForObject:#"payer_id"];) you actually get keys for an object that is not in any of the array's items. This #"player_id" is different object than the #"player_id" you added in current_row. In fact, maybe all of your rows have different #"player_id" objects (except if the compiler has made some optimization - maybe it threats that same string literal as one object instead of creating new object for each iteration).
Try creating an NSString object for the #"player_id" which you add to the current_row and then get all the keys for that same object:
NSString* playerId = #"player_id";
for(){
NSMutableArray *current_row = [NSMutableArray arrayWithObjects: playerId,...];
...
}
NSArray *newArray = [expense_ArrContents allKeysForObject:playerId];
Your NSArray *newArray = [expense_ArrContents allKeysForObject:#"payer_id"]; will not return any value because in expense_ArrContents there is no such key(#"payer_id"), instead there are keys like 1,2,3 etc.What is your requirement?Want to see what all keys are there in expense_ArrContents just log
NSArray*keys=[expense_ArrContents allKeys];
Try this :
NSMutableArray *array_key=[[NSMutableArray alloc]init];
for (NSString *key in expense_ArrContents) {
if ([[expense_ArrContents objectForKey:key] containsObject:#"payer_id"]) {
[array_key addObject:key];
}
}