【问题标题】:Convert NSString to NSDictionary - jsonserialization error 3840将 NSString 转换为 NSDictionary - jsonserialization 错误 3840
【发布时间】:2017-11-11 14:58:51
【问题描述】:

我有一个描述字符串作为输出-@"{\n \"40147089-db8b-4223-89ab-7a2f72ac57d6\" = {\n \"22337b85-d2cf-4079-8d5f-9804cb680f2c\" = (\n yes\n );\n \"87eeb49f-b34a-431b-8701-bd7f36bc91ea\" = (\n yes\n );\n };\n \"7262d43c-4d72-4e71-9097-6db5632d1f54\" = {\n \"22337b85-d2cf-4079-8d5f-9804cb680f2c\" = (\n yes\n );\n \"58ecb861-4372-412b-8740-a9f94260ed93\" = (\n yes\n );\n };\n}"

如何将其转换为NSDictionary?总是 jsonserialisation 向我抛出 Error Domain=NSCocoaErrorDomain Code=3840 "No value for key in object around character 45."

这就是我在打印 ResponseObj 时得到的结果,其中 ResponseObj 的类型为 id

po 响应对象

{
"159476cc-3e1f-4995-bf23-df41141dd8c5" = 2;
"6280ecfe-841e-4a35-8002-3e9e7327ba7d" = 2;
"64de3fc6-ff59-4e91-a2e7-f3ec904eecce" =     {
    "40147089-db8b-4223-89ab-7a2f72ac57d6" =         {
        "22337b85-d2cf-4079-8d5f-9804cb680f2c" =             (
            yes
        );
        "87eeb49f-b34a-431b-8701-bd7f36bc91ea" =             (
            yes
        );
    };
    "7262d43c-4d72-4e71-9097-6db5632d1f54" =         {
        "22337b85-d2cf-4079-8d5f-9804cb680f2c" =             (
            yes
        );
        "58ecb861-4372-412b-8740-a9f94260ed93" =             (
            yes
        );
    };
};
"83ec5c05-cb2f-4c49-b9a3-ab44c487bf55" =     {
    "2a8dd370-2f7e-4c8e-93d5-21102fbc82fd" =         {
        "957bdaba-b23d-4243-8384-62dfa46f0656" = work;
    };
    "b3e0aded-d57b-4159-9c33-c8b006282334" =         {
        "aba16653-bda0-4e89-b1c8-63df6faa7c10" =             (
            yes
        );
    };
    "d765038e-e85a-495d-9932-170852fbd86e" =         {
        "57646099-b717-4a2b-b9eb-2935548ae954" =             (
            yes
        );
        "957bdaba-b23d-4243-8384-62dfa46f0656" = play;
    };
};
"c81e7d71-6f1e-4b83-8740-c73ee52e3b0b" =     (
    2
);
}

【问题讨论】:

  • 您的字符串不是有效的 JSON。
  • 如前所述,JSOn 无效。使用类似这样的东西:jsonlint.com 来验证和检查错误
  • (yes) 例如,这很奇怪。否则,您可以使用 subString,并检查 45 周围的字符串是什么,例如 NSString *whereLiesIssue = [yourString substringWithRange:NSMakeRange(45-10,10)];。 “=”、“;”无效。我似乎更像是 Objective-C 中NSDictionary 的描述。
  • 您提供的字符串的有效 JSON 字符串如下所示:- "{\n \"40147089-db8b-4223-89ab-7a2f72ac57d6\" : {\n \"22337b85-d2cf- 4079-8d5f-9804cb680f2c\" : (\n 是\n );\n \"87eeb49f-b34a-431b-8701-bd7f36bc91ea\" : (\n 是\n );\n };\n \"7262d43c- 4d72-4e71-9097-6db5632d1f54\" : {\n \"22337b85-d2cf-4079-8d5f-9804cb680f2c\" : (\n 是\n );\n \"58ecb861-4372-412b-8740-a9f94260ed93\" : (\n 是\n );\n };\n}"
  • 我刚刚将 '=' 替换为 ':'

标签: objective-c nsstring nsdictionary nsjsonserialization


【解决方案1】:

请为您的数据使用 NSJSONSerialization。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多