Sending JSON object ios - ios

I'm new to iOS and looking for a little help. I am connecting to my socket server but having trouble emitting.
Heres how to emit:
if (self.socketIsConnected)
{
[self.socket emit:(NSString HERE) args:#[(NSArray HERE)]];
}
Heres what I tried:
if (self.socketIsConnected)
{
NSDictionary *deviceDic = #{#"username": #"drew", #"chatHash":#"FJHE8"};
[self.socket emit:#"adduser" args:#[[NSString stringWithFormat: #"%#", deviceDic]]];
}
The "args" param asks for a NSArray, but I need to send a JSON object that looks like this:
{"username": "drew", "chatHash":"FJHE8"}
How can I create this? And how do I put this object in the NSArray.
Update:
I tried this now
NSArray *keys = [NSArray arrayWithObjects:#"username", #"chatHash", nil];
NSArray *objects = [NSArray arrayWithObjects:#"drew", #"value2", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:keys];
[self.socket emit:#"adduser" args:dictionary];
and I looked on the server. It crashes my server with a throw exception. and the only data it receives is:
username

It appears that you're using SIOSocket. The documentation isn't very clear but looking at the source for the emit function (https://github.com/MegaBits/SIOSocket/blob/master/SocketIO/Source/SIOSocket.m), all you need to do is pass an NSArray with the first parameter being an NSDictionary. The dictionary will be serialized as a JSON object by the framework.
You can have multiple arguments in the emit call. The type of each parameter in argument depends on the types of objects in the array you pass. If you pass a dictionary, it'll convert it to a JSON object, an array to an array, numbers as numbers, strings as strings.

You can use
NSDictionary *subArgs = #{#"state" : #true};
[self.socket emit:#"adduser" args:#[subArgs]];
This will solve the problem.

Related

Adding object into NSMutableArray , crashing the App

Dict is coming from notification, taking out the NSData from dict and adding it to NSMutableArray is crashing the application.
Once in a while this crash is happening not always.
NSData *data=[dict objectForKey:#"obj"];
[self.RFTagData addObject:data];
You can directly add data object by doing this.Instead of converting to string.
Don't type cast NSData to NSString when adding objects into array.You should first convert NSData into NSString then add it to array.So
better way to use this NSData into NSString and add NSString into array.
NSData *data=[dict objectForKey:#"obj"];
NSString *strData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(data != nil self.RFTagData != nil)
{
[self.RFTagData addObject:strData];
.....
}
Example for Converting Data into String
You can directly get the data to array there is no need to cast.
if(self.RFTagData != nil){
self.RFTagData = [dict objectForKey:#"obj"];
}
NSLog(#"array %#", RFTagData);
This will add all data to array under the obj key.
Update:
As user rmaddy & danh suggested, so here needs to take concern over this point regarding use of valueForKey and objectForKey methods and nil check on the array.
objectForKey: This is an NSDictionary method. An NSDictionary is a collection class similar to an NSArray (collections), except instead of using indexes like NSArray, it uses keys to differentiate between items. A key is an arbitrary string you provide. No two objects can have the same key (just as no two objects in an NSArray can have the same index).
valueForKey: This is a KVC method. It works with ANY class. valueForKey: allows you to access a property using a string for its name.
Here both returns the value associated with a given key, so here using valueForKey method provides workaround solution to you. But using objectForKey is the more preferred way to use in such cases.
To check for the null values inside array which are identically appears like literals #"<null>" rather then NSNull objects typically used to represent nils in Cocoa collections. You can filter them out by using NSArray's filteredArrayUsingPredicate method:
NSPredicate *pred = [NSPredicate predicateWithBlock:^BOOL(id value, NSDictionary *unused) {
return ![str isEqualToString:#"<null>"];
}];
NSArray *filteredAry = [self.RFTagData filteredArrayUsingPredicate:pred];
NSLog(#"array with non null vals %#", filteredAry);

Convert NSmangedObject to JsonModel

In my application I am using a JsonModel for parsing JSON response from server and while storing it in Core Data I am using NSManagedObject and NSManagedContext which is provided by Apple itself. Now whenever I fetch I want to convert the NSManagedObject to JsonModel. Now the problem is I have to use two separate class to manage jsonModel and NSManagedObject.
You can get help from this link.
Get Core Data objects to JSON
Or in easy way from andrew-madsen's Answer
NSManagedObject *managedObject = ...;
NSArray *keys = [NSArray arrayWithObjects:#"key1", #"key2", ..., nil]; // These are the keys for the properties of your managed object that you want in the JSON
NSString *json = [[managedObject dictionaryWithValuesForKeys:keys] JSONRepresentation];
For more detail try this link
nsmanagedobject-to-json
Hope it helps you. first try to convert your NSMangedObject to NSDictionary.
NSArray *keys = [[[yourObject entity] attributesByName] allKeys];
NSDictionary *dict = [myObject dictionaryWithValuesForKeys:keys];
the you have to use dict as a JSON or convert them into JSON string if needed.

NSPropertyListSerialization returning nil instead of serialized data

I am putting two NSMutableArray objects into an NSDictionary and trying to serialize, but the method call is returning nil. One array, addresses, is an array of NSString objects. The other, engines is an array of objects that each contain several data types. I am attempting to serialize using the following code:
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:engAddr forKey:#"engAddr"];
[dictionary setObject:trainList forKey:#"engines"];
NSData *data = [NSPropertyListSerialization dataFromPropertyList:dictionary
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
Stepping through, the debugger shows the arrays are properly added to the dictionary, but after the line that should serialize the dictionary it shows data = (NSData *) nil.
Where am I going wrong? Thank you for your help!
What kind of objects does engines contain?
Plist supports only specific objects below.
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html#//apple_ref/doc/uid/10000048i-CH3-54303
If you want to serialize a custom object, convert it to NSData by NSKeyedArchiver.
To do that, objects must conform NSCoding protocol.

Use NSDictionary as other NSDictionary's key

i'm trying to achieve the following structure:
NSMutableDictionary *dict = [#{} mutableCopy];
NSDictionary *key1 = #{#"id_format": #(1), #"date": #"2014-08-01"};
NSDictionary *key2 = #{#"id_format": #(2), #"date": #"2014-08-02"};
// This runs perfect and can be checked in llvm debugger
// data1 & data2 are NSArray that contain several NSDictionary
[dict setObject:data1 forKey:key1];
[dict setObject:data2 forKey:key2];
// Later, if i try to access dict using another key, returns empty NSArray
NSDictionary *testKey = #{#"id_format": #(1), #"date": #"2014-08-01"}; // Note it's equal to "key1"
for(NSDictionary *dictData in dict[testKey]){
// dictData is empty NSArray
}
// OR
for(NSDictionary *dictData in [dict objectForKey:testKey]){
// dictData is empty NSArray
}
So the question is if is there possible to use NSDictionary as key, or not.
An object can be used as a key if it conforms to NSCopying, and should implement hash and isEqual: to compare by value rather than by identity.
Dictionaries follow the array convention of returning [self count] for hash. So it's a pretty bad hash but it's technically valid. It means your outer dictionary will end up doing what is effectively a linear search but it'll work.
Dictionaries implement and correctly respond to isEqual:. They also implement NSCopying.
Therefore you can use a dictionary as a dictionary key.

Replacing Object in Dictionary in an Array

I have an array structure as follows:
NSMutableArray topArray{
NSMutableArray middleArray{
NSMutableArray lowerArray{
NSMutableDictionary dict{
}
}
}
}
The array structure is filled with some data that I retrieve from the web is JSON format.
I am trying to edit one of the objects in the NSMutableDictionary as follow:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *lowerArray = [[self middleArray] objectAtIndex:[indexPath section]];
NSMutableDictionary *dict = [lowerArray objectAtIndex:[indexPath row]];
[dict setObject:[NSNumber numberWithInt:1] forKey:#"key"];
[[self tableView] reloadData];
}
The data within each of the arrays is correct (I have checked with print statements), however, when I try to change the object in the dict I get the following error:
reason:
'-[__NSCFDictionary setObject:forKey:]: mutating method sent to
immutable object'
I need the object in the dictionary to be changed within the array structure.
Could this be an issue with the JSON data since when topArray is first initialised with the JSON data the middle and lower arrays are in the form of just NSArray's?
Sorry if this is confusing, I will try to clarify more if you have any questions.
Thanks in advance for your help.
If you're using NSJSONSerialization, you can pass NSJSONReadingMutableContainers to the options parameter of +JSONObjectWithData:options:error:, and all of the parsed dictionaries and arrays will be mutable.
NSMutableArray *topArray = [NSJSONSerialization JSONObjectWithData:webServiceData
options:NSJSONReadingMutableContainers
error:nil];
Just use NSMutableDictionary class instead of just NSDictionary for the moduleDict variable. It is easily done when parsing JSON objects. If no - create one like this:
NSMutableDictionary *newDict = [NSMutableDictionary dictionaryWithDictionary: moduleDict];
I doubt that you are really dealing with NSMutableDictionary on the lowest level. Do you have the output from these print statements for us?
Usually the structures generated by JSON deserialisation are of immutable type. And that is exactly what your error message is telling.

Resources