是JSON不规范或标准使用@属性呢? [英] Is use of @attributes in JSON non-standard or standard?

查看:202
本文介绍了是JSON不规范或标准使用@属性呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在添加JSON输出到一个现有的API,输出XML,使MobileHTML整合容易得多。然而,我们的开发人员查询的使用出现在JSON输出@属性的。

We're adding JSON output to an existing API that outputs XML, to make MobileHTML integration much easier. However, our developer has queried the use of @attributes appearing in the JSON output.

我们的原始XML如下:

Our original XML looks like:

<markers>
    <marker id="11906" latitude="52.226578"
    ...

等等JSON出来的:

and so the JSON comes out as:

callbackname({"marker":[{"@attributes":{"id":"11906","latitude":"52.226578"
....

我们的开发者表示:

虽然'@属性是合法的JSON,这似乎打破点符号,所以我不能调用数据。@属性。我可以调用数据['@属性'],所以有一个变通方法,但它似乎安全只是为了避免@ -symbol,除非有一个很好的理由。

"Although '@attributes' is legal JSON, it seems to break dot notation, so I can't call data.@attributes. I can call data['@attributes'], so there's a workaround, but it seems safer just to avoid the @-symbol, unless there's a good reason for it."

该XML-> JSON(P)转换是通过使用:

The XML->JSON(P) conversion is done using:

$xmlObject = simplexml_load_string ($data);
$data = json_encode ($xmlObject);

我想使我们的API作为易于集成成为可能,因此,在这里可以使用标准的东西。但是,我们正在使用的原生PHP json_en code函数,因此,如果它做一些非标准我会感到惊讶。

I want to make our API as easy-to-integrate as possible, and therefore to use standard stuff where possible. But we're using the native PHP json_encode function, so I'd be surprised if it's doing something non-standard.

时使用@属性的非标?这基本上就是我们的API是有点用分列&LT的问题;标记ID ..&GT; ,而不是&LT;标记&GT; &LT;&ID GT;

Is use of @attributes non-standard? Is this basically just the problem that our API is a bit broken in terms of using <marker id..> rather than <marker><id> ?

推荐答案

借助 JSON标准只规定是什么,ISN 'T有效;它不设置约定。没有什么内在的错误使用的不是有效的JavaScript标识符的属性名称。

The JSON standard only specifies what is and isn't valid; it doesn't set convention. There's nothing inherently wrong with using property names which aren't valid Javascript identifiers.

然而,随着开发者所指出的,这也使它稍微不好用的结果JS,因为它使得它不可能用点号。 在握持手感,使用属性简单内容通常被视为好XML和你使用的默认情况下,内置的工具来转换从XML到JSON使用。我倾向于认为这是一个足够的理由离开它,因为它是。

However, as your developer points out, this does make it slightly more awkward to use the result in JS, as it makes it impossible to use dot notation. On the gripping hand, using attributes for simple content is usually seen as "good XML" and you're using using default, built-in tools to convert from XML to JSON. I'd tend to consider that a good enough reason to leave it as it is.

如果是我的话,我想看看这将是多么困难,实现自定义XML - > JSON转换器。如果是简单明了,走这条路,避免 @attribute (它也将有可能使你的JSON更小,更简单)。如果它是太多的麻烦,但是,在点符号错过了是不是世界的尽头。在最坏的情况, VAR ATTR = data.marker [@属性]; 会得到解决的问题。

If it were me, I'd look at how difficult it would be to implement a custom XML -> JSON converter. If it's simple and straightforward, go that route and avoid @attribute (it will also likely make your JSON smaller and simpler). If it's too much hassle, however, missing out on dot notation isn't the end of the world. At worst, var attr = data.marker["@attributes"]; will get around the issue.

这篇关于是JSON不规范或标准使用@属性呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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