JSON Error in iOS - iphone

i need to send json dictionary as parameter, i am using afnetworking. While using following code i am getting an exception:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json", nil];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
NSDictionary *params = #{#"email":txt_email.text,#"password":txt_password.text,#"platform":#"iphone"};
NSLog(#"params:%#",params);
SBJsonWriter *jsonWriter=[[SBJsonWriter alloc]init];
NSString *paramsDicJSONN = [jsonWriter stringWithObject:params];
NSString *str_url=[NSString stringWithFormat:#"%#do_signup",BaseURLlogin];
[manager POST:str_url parameters:paramsDicJSONN success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"offerJSON: %#", responseObject);
Exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write'

Read the AFNetworking documentation in regard to sending POST data.
- (AFHTTPRequestOperation *)POST:(NSString *)URLString parameters:(NSDictionary *)parameters ...
The parameters argument expects an NSDictionary, not a JSON encoded NSString. Most likely just pass params and skip all the SBJsonWriter code.

Related

Convert JSON String to Object

Trying to convert JSON String into the object
NSString *testString = #"OMJ=\"N.A.\"&SPJ=\"N.A.\"&OBJ=\"N.A.\"";
NSMutableDictionary *tags = [NSMutableDictionary new];
tags = [NSMutableDictionary dictionaryWithDictionary:[Utility convertJSONStringToObject:testString]];
Errror Message!
-[__NSCFConstantString objectFromJSONString]: unrecognized selector sent to instance!
After using the NSJSONSerialization the retun value for the dictionary JSON is null empty
NSDictionary *JSON =
[NSJSONSerialization JSONObjectWithData: [#"OMJ=\"N.A.\"&SPJ=\"N.A.\"&OBJ=\"N.A\"" dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: nil];
What i did wrong in my code. Any one advice me to fix the issue.
#thanks in advance

Reading value from JSON - help writing the logic

I have the JSON in the format of
{"index":"0","name":"jemmy","age":"2"}
I need to extract the values stored and save it; I have done the following, but it doesn't work
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]:
unrecognized selector sent to instance 0x6671a10'
my code
NSDictionary *dictionary = [request responseHeaders];
SBJsonParser *parser = [SBJsonParser new];
id content = [responseString JSONValue];
NSDictionary *dealDictionary = content;
NSArray *array = [dealDictionary allKeys];
for (NSString *str in array)
{
NSDictionary *childDictionary = [dealDictionary objectForKey:str];
NSLog(#"%# ",[childDictionary objectForKey:#"name"]);
}
You may not understand how to use SBJSON, maybe a quick read through the documentation would be useful. First, you need to actually use the parser that you create:
SBJsonParser *parser = [SBJsonParser new];
NSDictionary *content = [parser objectWithString:[request responseString]];
Secondly, you can traverse your dictionary slightly easier as such:
NSEnumerator *enum = [content keyEnumerator];
id key;
while (key = [enum nextObject]) {
NSLog(#"key %# and object %#",key,[content objectForKey:key]);
}

NSPropertyListSerialization after parsing an XML

I need to know if I am in the right track here. I am parsing an XML-RPC in iPhone (using the eczarny framework) and I am getting an array with objects. I create an NSData and store an object. After that I am trying to deserialize it but get en error.
Code:
NSArray *result = [response object];
NSData *data = [result objectAtIndex:0];
NSLog(#"Data %#",data);
NSDictionary * message = nil;
NSString * error = nil;
message = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListMutableContainers format:nil errorDescription:&error];
The nslog:
Data {
DESCRIPTION = "Standardverkn";
FLAGS = 0;
NAME = "Fenster OG3";
RECEIVER = "IEQ007:3";
SENDER = "IEQ0043:1";
}
The error:
-[__NSCFDictionary length]: unrecognized selector sent to instance 0x6e4bd50
What am I doing wrong?
[result objectAtIndex:0] is already an NSDictionary. You don't need to deserialize it. You can just directly use it as the message.
(If it is an NSData, the NSLog will show something like <12345678 9abcdef0 ...>.)

Problems with passing of data from NSDictionary to NSMutableDictionary

I'm trying to access a webservice using the json framework.
Code:
NSDictionary *allData;
NSMutableDictionary *displayData;
NSString *string = [[NSString alloc] initWithFormat:#"http://localhost/Gangbucks.svc/GetDealList"];
NSURL *jsonURL = [NSURL URLWithString:string];
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];
self.allData = [jsonData JSONValue];
NSLog(#"allData value is1:%#", self.allData);
self.displayData = [[NSMutableDictionary alloc] initWithDictionary:self.allData];
NSLog(#"displayData value is2:%#", self.displayData);
allData works fine but I got an error on the displayData
2011-12-13 14:59:23.875 IPhone[352:207] -[__NSArrayM getObjects:andKeys:]: unrecognized selector sent to instance 0x59e1c20
2011-12-13 14:59:23.878 IPhone[352:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM getObjects:andKeys:]: unrecognized selector sent to instance 0x59e1c20'
*** Call stack at first throw:
Is it possible to pass data from NSDictionary to NSMutableDictionary ?
allData is apparently an NSArray, not an NSDictionary. You should go back and look at your JSON. The top-level there is probably an array.

Cast JSON object (NSCFDictionary) to string and parse it

I'm working with facebook connect and trying to handle the JSON object that i'm receiving.
I invoked the requstWithGraphPath method and need to get back a JSON object,
tried to parse it and getting an error:
SBJSON *parser = [[SBJSON new] autorelease];
NSData *data = [[NSData alloc] initWithData:result];
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; -> in this line - "[__NSCFDictionary length]: unrecognized selector sent to instance"
NSArray *events = [parser objectWithString:jsonString];
What's the problem?
Can I get the string in an other way or parse the object differently?
Thanks.
If you are working with the delegate callback
- (void)request:(FBRequest *)request didLoad:(id)result;
the parsing work has been done for you. Traverse the NSDictionary or NSArray to find the data you are looking for. If you are working with the delegate callback
- (void)request:(FBRequest *)request didLoadRawResponse:(NSData *)data;
you should initialize an NSString with the data, and use the category method that SBJSON adds to NSString for creating an id. That is assuming the data is data that constructs a string.
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
id result = [jsonString JSONValue];
Are you sure the error happens on that line, or does it happen on the line above?
If result is an NSDictionary (or CFDictionary, same thing), then it is already parsed and you do not need to do that yourself — and it could cause that error message too, on the line above.
The line:
data = [[NSData alloc] initWithData:result];
is almost certainly not what you want to do, as it is equivalent to
data = [result copy];
assuming that result is an NSData object (or NSMutableData), which I'm guessing it isn't.