如何使用C样式注释解析json文件? [英] How to parse json file with c-style comments?

查看:142
本文介绍了如何使用C样式注释解析json文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json文件,例如:

I have a json file, such as the following:

    { 
       "author":"John",
       "desc": "If it is important to decode all valid JSON correctly \ 
and  speed isn't as important, you can use the built-in json module,   \
 orsimplejson.  They are basically the same but sometimes simplej \
further along than the version of it that is included with \
distribution."
       //"birthday": "nothing" //I comment this line
    }

此文件是由另一个程序自动创建的.如何使用Python进行解析?

This file is auto created by another program. How do I parse it with Python?

推荐答案

我无法想象json文件由其他程序自动创建" 会在其中包含注释.因为 json规范完全没有评论,所以

I can not imagine a json file "auto created by other program" would contain comments inside. Because json spec defines no comment at all, and that is by design, so no json library would output a json file with comment.

这些注释通常由人在以后添加.在这种情况下也不例外. OP在他的帖子中提到了这一点://"birthday": "nothing" //I comment this line.

Those comments are usually added later, by a human. No exception in this case. The OP mentioned that in his post: //"birthday": "nothing" //I comment this line.

真正的问题应该是,如何正确注释json文件中的某些内容,同时又保持其与规范的兼容性并因此与其他json库兼容?

So the real question should be, how do I properly comment some content in a json file, yet maintaining its compliance with spec and hence its compatibility with other json libraries?

答案是,将您的字段重命名为另一个名称.示例:

And the answer is, rename your field to another name. Example:

{
    "foo": "content for foo",
    "bar": "content for bar"
}

可以更改为:

{
    "foo": "content for foo",
    "this_is_bar_but_been_commented_out": "content for bar"
}

这在大多数情况下都可以正常工作,因为使用者很可能会忽略意外字段(但并非总是如此,这取决于您的json文件使用者的实现.因此,YMMV.)

This will work just fine most of the time because the consumer will very likely ignore unexpected fields (but not always, it depends on your json file consumer's implementation. So YMMV.)

更新:显然有些读者不满意,因为此答案并未给出他们期望的解决方案".好吧,事实上,我确实提供了一个可行的解决方案,方法是隐式链接到 JSON设计师的报价:

UPDATE: Apparently some reader was unhappy because this answer does not give the "solution" they expect. Well, in fact, I did give a working solution, by implicitly linking to the JSON designer's quote:

Douglas Crockford公开2012年4月30日用JSON注释

Douglas Crockford Public Apr 30, 2012 Comments in JSON

我从JSON中删除了注释,因为我看到人们正在使用它们来 持有解析指令,这种做法本来会破坏 互操作性.我知道缺乏评论使一些人 难过,但不应该.

I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability. I know that the lack of comments makes some people sad, but it shouldn't.

假设您正在使用JSON保留配置文件, 想要注释.继续并插入所有您喜欢的评论. 然后将其通过JSMin传递给JSON分析器.

Suppose you are using JSON to keep configuration files, which you would like to annotate. Go ahead and insert all the comments you like. Then pipe it through JSMin before handing it to your JSON parser.

是的,继续使用 JSMin .请记住,当您走向在JSON中使用注释"时,这在概念上是未知的领域.不能保证您选择的任何工具都能处理:内联[1,2,3,/* a comment */ 10],Python样式[1, 2, 3] # a comment(这是Python中的注释,但不是Javascript中的注释),INI样式[1, 2, 3] ; a comment,...,您明白了.

So, yeah, go ahead to use JSMin. Just keep in mind that when you are heading towards "using comments in JSON", that is a conceptually uncharted territory. There is no guarantee that whatever tools you choose would handle: inline [1,2,3,/* a comment */ 10], Python style [1, 2, 3] # a comment (which is a comment in Python but not in Javascript), INI style [1, 2, 3] ; a comment, ..., you get the idea.

我仍然建议首先不要在JSON中添加不兼容的注释.

I would still suggest to NOT adding noncompliant comments in JSON in the first place.

这篇关于如何使用C样式注释解析json文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆