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 ...>.)
Related
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
i have a NSData object.
I want to convert it to a string, pass to a function and then reconvert back to NSData object.
But How?
I tried this method, but NSData value it's different from the original!
here my code:
// a generic class
APClass *c = [[APClass alloc] init];
c.aNumber = 123;
c.aString = #"my string";
// my data obj
NSMutableData *data = [NSMutableData data];
// archiver to store class in nsdata
NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[encoder encodeObject:[NSNumber numberWithInt:c.aNumber] forKey:#"aNum"];
[encoder encodeObject:c.aString forKey:#"aStr"];
[encoder finishEncoding];
[encoder release];
[c release];
NSLog(#"%#", data);
NSString *d = [NSString stringWithFormat:#"%#", data];
// ---
NSString *strFromData = [NSString stringWithFormat:#"%#", d];
NSData *dataNM = [strFromData dataUsingEncoding:NSUTF8StringEncoding];
// decoder to retrieve class from nsdata
NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:dataNM];
int number = [[decoder decodeObjectForKey:#"aNum"] intValue];
NSString *string = [decoder decodeObjectForKey:#"aStr"];
[decoder finishDecoding];
[decoder release];
NSLog(#"[Number: %d] -- [String: %#]", number, string);
How can i convert back to original NSData?
data and dataNM are different in size.
Compiler give back this error:
2012-04-02 16:33:28.269 DataTest[18008:f803] -[__NSCFData
objectForKey:]: unrecognized selector sent to instance 0x6b46c80
2012-04-02 16:33:28.270 DataTest[18008:f803] * Terminating app due
to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFData objectForKey:]: unrecognized selector sent to instance
0x6b46c80'
thanks.
Solved.
Using dataUsingEncoding, value of NSData it's different.
To pass data around methods or apps, etc, i've used base64 convertion.
Encode
NSString *d =
[NSString stringWithFormat:#"appdue://obj=%#",
[APBase64Converter base64forData:data]];
Decode
NSData *data = [APBase64Converter base64DataFromString:urlParams];
APBase64Converter Is a lib that encode/decode easily data and strings.
Working example and APBase64Converter lib can be downloaded from here: http://goo.gl/8YNjS
Thanks all.
I "retain" this post for helps people and me next time!
Use
NSData to NSString:
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding
and
NSString to NSData:
- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding
with encoding
NSUTF8StringEncoding
or
NSMacOSRomanStringEncoding (which is an 8-bit encoding).
Use Base64 class it is available in github
DatabaseResult* result = [self.database executeQuery:#"SELECT * FROM `images` WHERE image_id = ? AND imagetype = ?", self.routeId,self.imagetype];
for(EGODatabaseRow* row in result) {
NSString *strImage=[row stringForColumn:#"image_photo"];
[Base64 initialize];
NSData *data = [Base64 decode:strImage];
UIImage *image=[UIImage imageWithData:data];
}
This is how i convert String to data and then data to image.
I have an IPhone application in which i am using this code .
NSPropertyListFormat plistFormat;
NSDictionary *payloadDict = [NSPropertyListSerialization
propertyListWithData:subscriptionProduct.receipt
options:NSPropertyListImmutable
format:&plistFormat
error:nil];
where i am getting the subscriptionProduct.receipt correctly and reciept is an nsdata, which is declared inside the subscriptionProduct class.But after the conversion when i am trying to print payloadDict it is terned to be null.can anybody help me
Try getting the error out of the method and displaying it:
NSPropertyListFormat plistFormat;
NSError *parseError;
NSDictionary *payloadDict = [NSPropertyListSerialization
propertyListWithData:subscriptionProduct.receipt
options:NSPropertyListImmutable
format:&plistFormat
error:&parseError];
NSLog(#"payloadDict = %#", payloadDict);
NSLog(#"parseError = %#", parseError);
If parseError is nil, the property list serialization thinks it read the data properly. If not, its contents should tell you where to look.
Your comments seem to say that you want to construct a new NSDictionary that stores the object subscriptionProduct.receipt.
You can do this:
NSMutableDictionary *payloadDict = [NSMutableDictionary dictionary];
// Always check if values are nil before adding them to a dictionary.
if (subscriptionProduct.receipt)
[payloadDict setObject:subscriptionProduct.receipt forKey:#"receipt"];
If you want to load the receipt later, you can do this:
NSData *receiptData = [payloadDict objectForKey:#"receipt"];
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]);
}
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.