猿问

XML与JSON用于保存数据

首先,我将使用C#编写一个应用程序,该应用程序将允许用户轻松地指定一系列带有参数和窗口位置的程序,命令或网站,然后单击即可启动它们。我试图确定使用XML或JSON编码用户文件是否会更好。


这是我对数据结构的初步设计:


XML格式


<AutoLauncher>

    <Program>

        <Location>C:\example.exe</Location>

        <Arguments>

            <Argument>

                <Key>a</Key>

                <Value>true</Value>

            </Argument>

        </Arguments>

        <Position>

            <Monitor>1</Monitor>

            <X>0</X>

            <Y>0</Y>

        </Position>

    </Program>

    <Cmd>

        <Command>example</Command>

        <Arguments>...</Arguments>

        <Position>...</Position>

    </Cmd>

    <Website>

        <Url>www.example.com</Url>

        <Browser>Default</Browser>

        <Arguments>...</Arguments>

        <Position>...</Position>

    </Website>

</AutoLauncher>

JSON格式


{

  Programs: [

    { Location: "C:\example.exe",

      Arguments: [

        {Key: "a", Value: true}

      ],

      Position: {

        Monitor: 1,

        X: 0,

        Y: 0

      }

    }

  ],

  Commands:[

    { Command: "example",

      Arguments:[...],

      Position: {...}

  ],

  Websites: [

    { Url: "www.example.com",

      Browser: "default",

      Arguments: [...],

      Position: {...}

  ]

}

注意:我在命令和网站的参数和位置中使用了...,因为其结构与程序结构完全一样。


哪种数据结构最好(或者还有其他更好的数据结构)?


慕码人2483693
浏览 242回答 2
2回答

潇潇雨雨

它肯定会在其他链接中介绍,但您的XML示例与JSON示例实质上相同。使用JSON,您几乎可以得到所有。借助XML,您可以更具表现力。例如:<!-- My AutoLauncher Settings : Last updated 2018-04-16 --><AutoLauncher>&nbsp; &nbsp; <Program Location="C:\example.exe" Monitor="1" X="0" Y="0">&nbsp; &nbsp; &nbsp; &nbsp; <Arguments>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Argument Key="a" Value="true" />&nbsp; &nbsp; &nbsp; &nbsp; </Arguments>&nbsp; &nbsp; </Program>&nbsp; &nbsp; <!-- Temporarily disable this one. I'll fix later.&nbsp; &nbsp; <Cmd Command="example" Monitor="1" X="0" Y="0">&nbsp; &nbsp; &nbsp; &nbsp; <Arguments>...</Arguments>&nbsp; &nbsp; &nbsp; &nbsp; <Position>...</Position>&nbsp; &nbsp; </Cmd>&nbsp; &nbsp; -->&nbsp; &nbsp; <!-- I want to launch this because... -->&nbsp; &nbsp; <Website Url="www.example.com" Browser="Default" Monitor="1" X="0" Y="0">&nbsp; &nbsp; &nbsp; &nbsp; <Arguments>...</Arguments>&nbsp; &nbsp; </Website></AutoLauncher>JSON有它的用途。但是对于配置(特别是用户管理),我更喜欢XML。

慕工程0101907

JSON的结构直观,可以轻松使用任何编程语言读取和直接映射到域对象。
随时随地看视频慕课网APP
我要回答