iOS 的 JSON 反序列化

发布于 2024-12-13 18:46:56 字数 538 浏览 0 评论 0原文

我正在尝试为我正在编写的 iOS 应用程序反序列化 JSON 字符串。但是,我的应用程序始终挂在我尝试读取的提要中双引号的“\”转义序列上。有什么快速方法可以删除该序列以便我可以正确解析提要? (提要来自服务器端的 Django 应用程序。)

这是我的 json 字符串:

[{\"pk\": 4161, \"model\": \"news.article\", \"fields\": {\"date_live\": \"2011-11-03 00:00:01\", \"date_added\": \"2011-11-03 03:10:59\", \"date_modified\": \"2011-11-03 03:10:59\", \"slug\": \"thursday-feature-backup-comic\", \"title\": \"THURSDAY FEATURE --> Backup Comic\"}}

您会注意到我的所有“节点”都有用于引用的“\”转义序列。有什么想法吗?

在此先感谢您提供的任何帮助。

L。

I am trying to deserialize a JSON string for an iOS app that I am writing. However, my app keeps getting hung up on the "\" escape sequence for double-quotes in the feed I am trying to read. What is a quick way to strip out that sequence so I can parse the feed correctly? (The feed is coming from my Django application on the server-side.)

Here is my json string:

[{\"pk\": 4161, \"model\": \"news.article\", \"fields\": {\"date_live\": \"2011-11-03 00:00:01\", \"date_added\": \"2011-11-03 03:10:59\", \"date_modified\": \"2011-11-03 03:10:59\", \"slug\": \"thursday-feature-backup-comic\", \"title\": \"THURSDAY FEATURE --> Backup Comic\"}}

You will notice that all of my "nodes" have the "\" escape sequence for the quote. Any ideas?

Thank you in advance for any help here.

L.

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

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

发布评论

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

评论(1

若水微香 2024-12-20 18:46:56

如果您尝试删除 python 中的转义符:

import re, json
myjson = re.sub(r"\\", "", "[{\"pk\": 4161, \"model\": \"news.article\", \"fields\": {\"date_live\": \"2011-11-03 00:00:01\", \"date_added\": \"2011-11-03 03:10:59\", \"date_modified\": \"2011-11-03 03:10:59\", \"slug\": \"thursday-feature-backup-comic\", \"title\": \"THURSDAY FEATURE --> Backup Comic\"}}]")
myjson = json.loads(myjson) # will decode json

但如果您想删除 javascript 中的转义符:

myjson = "[{\"pk\": 4161, \"model\": \"news.article\", \"fields\": {\"date_live\": \"2011-11-03 00:00:01\", \"date_added\": \"2011-11-03 03:10:59\", \"date_modified\": \"2011-11-03 03:10:59\", \"slug\": \"thursday-feature-backup-comic\", \"title\": \"THURSDAY FEATURE --> Backup Comic\"}}]";
myjson = eval(myjson.replace(/\\/, "")); // will encode json

注意: 您忘记了字符串末尾的 ] 。至少当我尝试评估它时我遇到了错误。我把 ] 放在最后并且 eval 工作了。

If you are trying to remove the escape inside python:

import re, json
myjson = re.sub(r"\\", "", "[{\"pk\": 4161, \"model\": \"news.article\", \"fields\": {\"date_live\": \"2011-11-03 00:00:01\", \"date_added\": \"2011-11-03 03:10:59\", \"date_modified\": \"2011-11-03 03:10:59\", \"slug\": \"thursday-feature-backup-comic\", \"title\": \"THURSDAY FEATURE --> Backup Comic\"}}]")
myjson = json.loads(myjson) # will decode json

But if you want to remove the escape from javascript:

myjson = "[{\"pk\": 4161, \"model\": \"news.article\", \"fields\": {\"date_live\": \"2011-11-03 00:00:01\", \"date_added\": \"2011-11-03 03:10:59\", \"date_modified\": \"2011-11-03 03:10:59\", \"slug\": \"thursday-feature-backup-comic\", \"title\": \"THURSDAY FEATURE --> Backup Comic\"}}]";
myjson = eval(myjson.replace(/\\/, "")); // will encode json

Note: You forgot a ] at the end of your string. At least I had an error when I've tryed to eval it. I put the ] at the end and eval worked.

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