I have the following JSON array:
[u'steve@gmail.com']
“u” is apparently the unicode character, and it was automatically created by Python. Now, I want to bring this back into Objective-C and decode it into an array using this:
+(NSMutableArray*)arrayFromJSON:(NSString*)json
{
if(!json) return nil;
NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
//I've also tried NSUnicodeStringEncoding here, same thing
NSError *e;
NSMutableArray *result= [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e];
if (e != nil) {
NSLog(@"Error:%@", e.description);
return nil;
}
return result;
}
However, I get an error: (Cocoa error 3840.)" (Invalid value around character 1.)
How do I remedy this?
Edit: Here’s how I bring the entity from Python back into objective-c:
First I convert the entity to a dictionary:
def to_dict(self):
return dict((p, unicode(getattr(self, p))) for p in self.properties()
if getattr(self, p) is not None)
I add this dictionary to a list, set the value of my responseDict[‘entityList’] to this list, then self.response.out.write(json.dumps(responseDict))
However the result I get back still has that ‘u’ character.
[u’steve@gmail.com’] is the decoded python value of the array it is not valid JSON.
The valid JSON string data would be just
["steve@gmail.com"].Dump the data from python back into a JSON string by doing:
The
uprefix on python string literals indicates that those strings are unicode rather than the default encoding in python2.X (ASCII).