在 Objective-C 中解析格式不良的 JSON

发布于 2025-01-03 23:40:37 字数 8490 浏览 0 评论 0原文

我的任务是解析一些 JSON,这些 JSON 是通过在 XML WADL 上运行 XML 到 JSON 转换工具创建的。我能够将响应中返回的数据解析为 JSON,并且能够从数据中获取顶级 NSDictionary。我可以从字典中获取一个数组,并从数组中获取一个 NSDictionary (它包含键值,但仅此而已),这就是我所能达到的深度。我过去已经解析过,但是这个特定的 JSON 格式我没有运气?这是我正在使用的代码..

// Decode the data
NSError *parseError = nil;
NSData *jsonData = [request responseData];
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&parseError];
if (parseError) {
    NSLog(@"Error: %@", [parseError localizedDescription]);

    // other stuff..
    return;
}

NSLog(@"JSON=: %@", responseDict.description);
NSArray *messageBoxResponse = [responseDict objectForKey:@"messageBoxResponse"];
NSLog(@"messageBoxResponse=: %@", messageBoxResponse);
NSUInteger count = messageBoxResponse.count;
for (NSDictionary* messages in messageBoxResponse) {

    NSLog(@"Messages=: %@", messages.description);
    NSArray *message = [messages objectForKey:@"message"];

    // get SIGABRT
    count = message.count;

    // If I comment out above line and introduce this I get SIGABRT also
    for (NSDictionary* something in message) {
        NSLog(@"Somthing=: %@", something.description);


}

这是我试图解析的 JSON..IP 已更改:):

{
messageBoxResponse =     {
    messages =         {
        link =             {
            href = "http://1.1.1.1:80/services/messageboxes/63358/messages";
            rel = self;
        };
        message =             (
                            {
                flags =                     {
                    answered = 0;
                    link =                         {
                        href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/flags";
                        rel = self;
                    };
                    taggedForDeletion = 0;
                    unread = 0;
                };
                from = "[email protected]";
                link =                     {
                    href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>";
                    rel = self;
                };
                parts =                     {
                    link =                         {
                        href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts";
                        rel = self;
                    };
                    part =                         {
                        link =                             {
                            href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts/0";
                            rel = self;
                        };
                        mimeType = "TEXT/PLAIN";
                        name = "text.txt";
                        size = 564;
                    };
                };
                receivedDate = 1328624061000;
                sentDate = 1328624035000;
                subject = test;
            },
                            {
                flags =                     {
                    answered = 0;
                    link =                         {
                        href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/flags";
                        rel = self;
                    };
                    taggedForDeletion = 0;
                    unread = 0;
                };
                from = "[email protected]";
                link =                     {
                    href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>";
                    rel = self;
                };
                parts =                     {
                    link =                         {
                        href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts";
                        rel = self;
                    };
                    part =                         (
                                                    {
                            link =                                 {
                                href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts/0";
                                rel = self;
                            };
                            mimeType = "TEXT/PLAIN";
                            name = "text.txt";
                            size = 6;
                        },
                                                    {
                            content =                                 {
                                href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts/1/2c3a3400620f218d5378607260dc2749.wav";
                                rel = content;
                            };
                            duration = 3;
                            link =                                 {
                                href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts/1";
                                rel = self;
                            };
                            mimeType = "AUDIO/WAV";
                            name = "Audio_Recording_S000551_002.wav";
                            size = 31190;
                        },
                                                    {
                            link =                                 {
                                href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts/2";
                                rel = self;
                            };
                            mimeType = "APPLICATION/MS-TNEF";
                            name = "winmail.dat";
                            size = 656;
                        }
                    );
                };
                receivedDate = 1328562604000;
                sentDate = 1328562596000;
                subject = "Voice Message from Suren 1 (63357)";
            }
        );
    };
};

}

My task is to parse some JSON which was created by running an XML to JSON conversion tool on an XML WADL. I'm able to parse the data returned in the response into JSON and I'm able to obtain the top level NSDictionary from the data. I can get an array from the dictionary and an NSDictionary from the Array ( it includes the key value but thats it) and thats as deep as I can go. I've done parsing in the past but this particular JSON format I am having no luck? Here is the code I'm using..

// Decode the data
NSError *parseError = nil;
NSData *jsonData = [request responseData];
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&parseError];
if (parseError) {
    NSLog(@"Error: %@", [parseError localizedDescription]);

    // other stuff..
    return;
}

NSLog(@"JSON=: %@", responseDict.description);
NSArray *messageBoxResponse = [responseDict objectForKey:@"messageBoxResponse"];
NSLog(@"messageBoxResponse=: %@", messageBoxResponse);
NSUInteger count = messageBoxResponse.count;
for (NSDictionary* messages in messageBoxResponse) {

    NSLog(@"Messages=: %@", messages.description);
    NSArray *message = [messages objectForKey:@"message"];

    // get SIGABRT
    count = message.count;

    // If I comment out above line and introduce this I get SIGABRT also
    for (NSDictionary* something in message) {
        NSLog(@"Somthing=: %@", something.description);


}

Here is the JSON I'm attempting to parse.. IPs have been changed :):

{
messageBoxResponse =     {
    messages =         {
        link =             {
            href = "http://1.1.1.1:80/services/messageboxes/63358/messages";
            rel = self;
        };
        message =             (
                            {
                flags =                     {
                    answered = 0;
                    link =                         {
                        href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/flags";
                        rel = self;
                    };
                    taggedForDeletion = 0;
                    unread = 0;
                };
                from = "[email protected]";
                link =                     {
                    href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>";
                    rel = self;
                };
                parts =                     {
                    link =                         {
                        href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts";
                        rel = self;
                    };
                    part =                         {
                        link =                             {
                            href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts/0";
                            rel = self;
                        };
                        mimeType = "TEXT/PLAIN";
                        name = "text.txt";
                        size = 564;
                    };
                };
                receivedDate = 1328624061000;
                sentDate = 1328624035000;
                subject = test;
            },
                            {
                flags =                     {
                    answered = 0;
                    link =                         {
                        href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/flags";
                        rel = self;
                    };
                    taggedForDeletion = 0;
                    unread = 0;
                };
                from = "[email protected]";
                link =                     {
                    href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>";
                    rel = self;
                };
                parts =                     {
                    link =                         {
                        href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts";
                        rel = self;
                    };
                    part =                         (
                                                    {
                            link =                                 {
                                href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts/0";
                                rel = self;
                            };
                            mimeType = "TEXT/PLAIN";
                            name = "text.txt";
                            size = 6;
                        },
                                                    {
                            content =                                 {
                                href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts/1/2c3a3400620f218d5378607260dc2749.wav";
                                rel = content;
                            };
                            duration = 3;
                            link =                                 {
                                href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts/1";
                                rel = self;
                            };
                            mimeType = "AUDIO/WAV";
                            name = "Audio_Recording_S000551_002.wav";
                            size = 31190;
                        },
                                                    {
                            link =                                 {
                                href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts/2";
                                rel = self;
                            };
                            mimeType = "APPLICATION/MS-TNEF";
                            name = "winmail.dat";
                            size = 656;
                        }
                    );
                };
                receivedDate = 1328562604000;
                sentDate = 1328562596000;
                subject = "Voice Message from Suren 1 (63357)";
            }
        );
    };
};

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮云落日 2025-01-10 23:40:37

这是因为 json 无效。它应该看起来更像这样:

{
    "messages": {
        "link": {
            "href": "http: //1.1.1.1: 80/services/messageboxes/63358/messages",
            "rel": "self"
        }
    }
}

“=”需要是“:”,有不应该存在的分号。

Its because the json is not valid. It should look more like this:

{
    "messages": {
        "link": {
            "href": "http: //1.1.1.1: 80/services/messageboxes/63358/messages",
            "rel": "self"
        }
    }
}

the '=' need to be ':' there are semicolons that shouldn't be there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文