JSON 发布,我是否把 JSON 推得太远了?

2024-01-24

我只是想知道我是否把 JSON 推得太远了?如果有人以前打过这个?

我有一个 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<customermodel:Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:customermodel="http://customermodel" xmlns:personal="http://customermodel/personal" id="1" age="1" name="Joe">
<bankAccounts xsi:type="customermodel:BankAccount" accountNo="10" bankName="HSBC" testBoolean="true" testDate="2006-10-23" testDateTime="2006-10-23T22:15:01+08:00" testDecimal="20.2" testTime="22:15:01+08:00">
    <count>0</count>
    <bankAddressLine>HSBC</bankAddressLine>
    <bankAddressLine>London</bankAddressLine>
    <bankAddressLine>31 florence</bankAddressLine>
    <bankAddressLine>Swindon</bankAddressLine>
  </bankAccounts>
</customermodel:Customer>

其中包含元素和属性......

当我转换为 JSON 时,会给出:

{"customermodel:Customer":{"id":"1","name":"Joe","age":"1","xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","bankAccounts":{"testDate":"2006-10-23","testDecimal":"20.2","count":"0","testDateTime":"2006-10-23T22:15:01+08:00","bankAddressLine":["HSBC","London","31 florence","Swindon"],"testBoolean":"true","bankName":"HSBC","accountNo":"10","xsi:type":"customermodel:BankAccount","testTime":"22:15:01+08:00"},"xmlns:personal":"http://customermodel/personal","xmlns:customermodel":"http://customermodel"}}

然后我也将其发送给客户端..它转换为js对象(或其他)编辑一些值(元素),然后将其发送回服务器。

所以我得到 JSON 字符串,并将其转换回 XML:

<customermodel:Customer>
    <id>1</id>
    <age>1</age>
    <name>Joe</name>
    <xmlns:xsi>http://www.w3.org/2001/XMLSchema-instance</xmlns:xsi>
    <bankAccounts>
        <testDate>2006-10-23</testDate>
        <testDecimal>20.2</testDecimal>
        <testDateTime>2006-10-23T22:15:01+08:00</testDateTime>
        <count>0</count>
        <bankAddressLine>HSBC</bankAddressLine>
        <bankAddressLine>London</bankAddressLine>
        <bankAddressLine>31 florence</bankAddressLine>
        <bankAddressLine>Swindon</bankAddressLine>
        <accountNo>10</accountNo>
        <bankName>HSBC</bankName>
        <testBoolean>true</testBoolean>
        <xsi:type>customermodel:BankAccount</xsi:type>
        <testTime>22:15:01+08:00</testTime>
    </bankAccounts>
    <xmlns:personal>http://customermodel/personal</xmlns:personal>
    <xmlns:customermodel>http://customermodel</xmlns:customermodel>
</customermodel:Customer>

还有一个问题,似乎不知道元素/属性之间的区别,所以我无法检查 XSD 来检查它现在是否有效?

有针对这个的解决方法吗?

我不能是第一个遇到这个问题的人吗?


JSON 作为 XML 编码没有意义,没有。如果您想使用和操作 XML,那么就使用和操作 XML。

JSON 适合当您需要更轻量、更易于解析、更易于编写和阅读的内容时。它有一个相当简单的结构,与 XML 相比既不好也不坏,只是不同而已。它具有列表、关联、字符串和数字,而 XML 具有嵌套元素、属性和实体。虽然您可以将其中一个精确地编码到另一个中,但您必须问自己为什么要这样做;如果您想要 JSON,请使用 JSON;如果您想要 XML,请使用 XML。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JSON 发布,我是否把 JSON 推得太远了? 的相关文章

随机推荐