SBJsonParser in iPhone app - iphone

I'm working with JSON library and see this situation:
Convert JSON string to NSDictionary
Scenario 1:
NSString *jsonString = #"{\"Name\":\"Foo\", Points:5}";
NSDictionary *dictionary = (NSDictionary*)[jsonParser objectWithString:jsonString];
NSLog(#"Dictionary: %#",dictionary);
I see the result as followed:
Dictionary: {
Name = "Foo";
Points = 5;
}
So that's correct.
Scenario 2:
NSString *jsonString = #"{\"Name\":\"Foo\", Points:0.5}";
NSDictionary *dictionary = (NSDictionary*)[jsonParser objectWithString:jsonString];
NSLog(#"Dictionary: %#",dictionary);
I see the result as followed:
Dictionary: {
Name = "Foo";
Points = "0.5";
}
???
Scenario 3:
NSString *jsonString = #"{\"Name\":\"Foo\", Points:-1}";
NSDictionary *dictionary = (NSDictionary*)[jsonParser objectWithString:jsonString];
NSLog(#"Dictionary: %#",dictionary);
I see the result as followed:
Dictionary: {
Name = "Foo";
Points = "-1";
}
???
Why does the JSON library convert the negative numbers or number less than 1 into string?
Do you know how to prevent this from happening?

I do not have the "why", but it may not be an issue for you since you can call for the intValue or floatValue when retrieving.
NSLog(#"Points = %.2f", [[dictionary valueForKey:#"Points"] floatValue]);

Related

trouble in fetching object from NSDictionary

i am making dictionary with 2 array one for keys(ids that you can see in Dic) and one for values(Birth dates that you can see in Dic) my dictionary look like this
100000297621293 = "08/31/1990";
100001904155266 = "12/30/1990";
100003248631105 = "05/27/1990";
100004327360299 = "01/01/1927";
100000157646688 = "08/22/1989";
100001069796883 = "12/03/1989";
100001475514001 = "03/09/1990";
100000717474427 = "08/05/1990";
100001221367192 = "08/05/1990";
100002586744158 = "04/15/1983";
this is just sample dic not full
then after i have another array with ids and im using that array for fetching birth dates from this Dic but i get null values plz help me my code is as below
NOTE: ARRAY WHICH I AM USING FOR KEY, ALWAYS EXIST AS KEY IN DIC
NSDictionary *birthdayDictionary =
[[NSDictionary alloc] initWithObjects:_parssedArrayOfFaceBook forKeys:_parssedArrayOfFaceBookUid];
NSLog(#"asdas%#",birthdayDictionary.description);
NSMutableArray *matches = [NSMutableArray array];
for (NSString *key in _selecteduid) {
NSLog(#" see it%#",key);
NSString *selectedBirthDate = [birthdayDictionary objectForKey:key];
NSLog(#" matched%#",selectedBirthDate);
[matches addObject:selectedBirthDate];
NSLog(#" matched%#",matches);
}
Why is *selectedBirthDate an NSMutableArray? The birth dates (values) are strings! not arrays!
NSString *selectedBirthDate = [birthdayDictionary objectForKey:key];
Assuming selecteduid is a retained property and it has values in it, this code would work fine.
NSDictionary *birthdayDictionary = [NSDictionary dictionaryWithObjects:self.parssedArrayOfFaceBook
forKeys:self.parssedArrayOfFaceBookUid];
NSMutableArray *matches = [#[] mutableCopy];
for (NSString *key in self.selecteduid){
NSString *selectedBirthDate = birthdayDictionary[key];
[matches addObject:selectedBirthDate];
}

iphone objective-c : string to NSData conversion for json deserialization

I receive json from a webservice into a NSMutableData.
That gets converted into a NSDictionary using TouchJson.
NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:responseData error:&error];
NSString *strData = [dictionary objectForKey:#"cars"];
I then retrieve a string from a key from that dictionary.
The string looks like below
{
b = "http://schemas.datacontract.org/";
car = (
{
"car_name" = "Honda Civic";
year = 2011;
"dealer" = "local honda dealer";
"bought on" = {
nil = 1;
};
"license_number" = 1234567;
status = ReadyToGo;
}
)};
Essentially there can be 'n' records against the 'car' key.
when I try to convert the above to NSData using
NSData *jsonData = [strData dataUsingEncoding:NSUTF8StringEncoding];
and also
NSData *jsonData = [strData dataUsingEncoding:[NSString defaultCStringEncoding]];
but I get
[__NSCFDictionary dataUsingEncoding:]: unrecognized selector sent to instance 0x532bb70
I have tried a few other encodings available and xcode still threw up.
How can I figure out the encoding being used?
This is my first attempt at deserealizing json in objective-c.
What am I missing/doing wrong here?
Thanks
I think it's not a string at all....
change to this and test....
NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:responseData error:&error];
NSDictionary *carsDictionary = [dictionary objectForKey:#"cars"];
NSArray *arrayOfCarDictionaries = [carsDictionary objectForKey:#"car"];

key values from an NSDictionary formatting with "()"

I'm trying to pull two values from this Dictionary, But the values I'm getting have "()" around them. Any Ideas what is causing this?
Here is the ServerOutput:
{"Rows":[{"userid":"1","location":"beach"}]}
Dictionary after JSON:
{
Rows = (
{
location = beach;
userid = 1;
}
);
}
This is what I'm getting:
location : (
beach
)
user Id : (
1
)
Both the userid and the location key values have the "()". Here is the code. Thanks a lot.
NSString *serverOutput= [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
if(serverOutput > 1){
SBJSON *jsonFF = [[SBJSON new] autorelease];
NSError *error3 = nil;
NSDictionary *useridDict= [jsonFF objectWithString:serverOutput error:&error3];
NSLog(#"useridDict: %#",useridDict);
idreturn = [[useridDict valueForKey:#"Rows"] valueForKey:#"userid"];
locationreturn = [[useridDict valueForKey:#"Rows"] valueForKey:#"location"];
NSLog(#" user Id : %#", idreturn);
NSLog(#" location : %#", locationreturn);
Just to clarify what is going on. When parsing JSON {} gets returned as a dictionary and [] gets retured as an array. So we have useridDict an NSDictionary containing the parsed data.
'useridDict' has one key Rows which returns an NSArray.
NSArray *useridArray = [useridDict objectForKey:#"Rows"];
Our useridArray has one element, an NSDictionary
NSDictionary *dict = [useridArray objectAtIndex:0];
This dict contains the two keys: location and userid
NSString *location = [dict objectForKey:#"location"];
NSInteger userid = [[dict objectForKey:#"userid"] intValue];
You can use like this.
idreturn = [[[useridDict valueForKey:#"Rows"] objectAtIndex:0]valueForKey:#"userid"];
locationreturn = [[[useridDict valueForKey:#"Rows"] objectAtIndex:0] valueForKey:#"location"];

How to get key value from NSdictonary - iphone sdk

I use an API which provides me data from the web. The data is in JSON format, and is stored in a NSDictionary. Like this:
SBJSON *parser = [[SBJSON alloc] init];
dict = [[NSDictionary alloc]init];
dict = [parser objectWithString:jsonArray error:nil];
Ggb console result for: po dict
1262 = {
"feed_name" = name1;
"new_results" = 6;
"next_update" = "2010-02-16T15:22:11+01:00";
};
1993 = {
"feed_name" = name2;
"new_results" = 0;
"next_update" = "2010-02-16T09:09:00+01:00";
};
How can I access the values "1262" and "1993" and put them in a NSArray, for use in a UITableView?
First of all, SBJSON's objectWithString:error: method will return one of the folowing depending on what the root object is in the JSON message:
NSArray
NSDictionary
So, you shouldn't always assume it'll return you a dictionary unless you know for a fact what will be returned in the JSON.
Secondly, you're allocating a new NSDictionary object, but then assigning the result of the parser to your dict variable, leaking the previously allocated dictionary.
You don't need this line: dict = [[NSDictionary alloc]init];.
Finally, since the returned object is a dictionary, you can get al of the objects out of the dictionary like this:
for (NSString *key in [dict allKeys])
{
NSDictionary *feed = [dict objectForKey:key];
//do stuff with feed.
}
return [dict allKeys];

A question regarding to the badge

I have
NSDictionary *dictionary =
{"aps":
{"alert":"This is a Push Message sent from Server!",
"badge":60
}
}
I have only 1 question
I need 60 from this NSDictionary into NString.
You can just get the NSNumber and format it as a string, something along the lines of:
NSDictionary *aps = [dictionary objectForKey:#"aps"];
NSNumber *num = [aps objectForKey:#"badge"];
NSString *numStr = [num stringValue];