本文介绍了JSON值的反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在反序列化JSON值时遇到问题.

I have problems with deserialization of a JSON-Value.

这是我的代码:

NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData: data options:0 error: &errorJson];
NSString *innerJson = responseDict[@"d"];
NSMutableDictionary *innerObject = [innerJson JSONValue];

如您所见,在休闲的打印屏幕(在执行上述代码的最后一行之后立即获取)中,字典innerObject中的项目包含特殊字符,这些特殊字符在innerJson中不存在.有人可以帮我,为什么会这样?

As you can see in the fallowing printscreen (which is taken right after executing the last line of the code above), the items in the dictionary innerObject contains special characters, which are not there in innerJson. Can someone help me, why this occurs?

编辑-添加了控制台中变量的说明

innerJson:

{"ret" : "1",  "msg" : "",  "list" : ["Granatapfel¤200g¤1", "Brombeeren¤300g¤1", "Papaya (100 g)¤3¤0", "EPF Müesli 1 Messlöffel¤2¤1", "grüner Spargel (190 g)¤2¤1", "Chicorée (130 g)¤1¤0", "mageres Kalbfleisch (190 g)¤3¤0", "Zander (160 g)¤6¤0", "Bachsaibling (190 g)¤2¤0", "Seeteufel (160 g)¤1¤0", "EPF Guetzli Hafer/\nKartoffel-Dinkel 3 Stk.¤0¤0"]}

内部对象:

{
    list =     (
        "Granatapfel\U00a4200g\U00a41",
        "Brombeeren\U00a4300g\U00a41",
        "Papaya (100 g)\U00a43\U00a40",
        "EPF M\U00fcesli 1 Messl\U00f6ffel\U00a42\U00a41",
        "gr\U00fcner Spargel (190 g)\U00a42\U00a41",
        "Chicor\U00e9e (130 g)\U00a41\U00a40",
        "mageres Kalbfleisch (190 g)\U00a43\U00a40",
        "Zander (160 g)\U00a46\U00a40",
        "Bachsaibling (190 g)\U00a42\U00a40",
        "Seeteufel (160 g)\U00a41\U00a40",
        "EPF Guetzli Hafer/\nKartoffel-Dinkel 3 Stk.\U00a40\U00a40"
    );
msg = "";
ret = 1;

}

推荐答案

我看不到您的JSON值有问题. NSStringdescription方法逐字打印所有Unicode字符,例如

I cannot see a problem with your JSON value. The description method of NSStringprints all Unicode characters verbatim, e.g.

"Granatapfel¤200g¤1"

另一方面,NSDictionaryNSArraydescription方法显示所有非ASCII字符作为\Unnnn转义,例如\U00a4代替¤:

On the other hand, the description method of NSDictionary and NSArray prints all non-ASCII charactersas a \Unnnn escape, e.g. \U00a4 instead of ¤:

"Granatapfel\U00a4200g\U00a41"

如果您从innerObject中提取字符串值,例如

If you extract the string values from innerObject, e.g.

NSString *s = innerObject["list"][0];
NSLog(@"%@", s);

然后您将看到所有值都已正确存储.

then you will see that all values are stored correctly.

这篇关于JSON值的反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 00:58