Sno*_*man 2 python cocoa-touch json objective-c ios
我有以下JSON数组:
[u'steve@gmail.com']
Run Code Online (Sandbox Code Playgroud)
"你"显然是unicode角色,它是由Python自动创建的.现在,我想将其重新引入Objective-C并使用以下方法将其解码为数组:
+(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;
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到一个错误: (Cocoa error 3840.)" (Invalid value around character 1.)
我该如何解决这个问题?
编辑:这是我如何将Python中的实体带回objective-c:
首先,我将实体转换为字典:
def to_dict(self):
return dict((p, unicode(getattr(self, p))) for p in self.properties()
if getattr(self, p) is not None)
Run Code Online (Sandbox Code Playgroud)
我将此字典添加到列表中,然后将my responseDict ['entityList']的值设置为此列表 self.response.out.write(json.dumps(responseDict))
然而,我得到的结果仍然是'u'字符.
[u'steve@gmail.com']是数组的解码python值,它是无效的JSON.
有效的JSON字符串数据就是这样["steve@gmail.com"].
通过执行以下操作将数据从python转储回JSON字符串:
import json
python_data = [u'steve@gmail.com']
json_string = json.dumps(data)
Run Code Online (Sandbox Code Playgroud)
upython字符串文字的前缀表示这些字符串是unicode而不是python2.X(ASCII)中的默认编码.
| 归档时间: |
|
| 查看次数: |
2711 次 |
| 最近记录: |