如何以JSON格式表示数据?

时间:2014-01-18 08:55:00

标签: java json

我在这里有一个非常基本的问题。我试图以JSON格式表示以下数据。

haschanged=true
version=1
timestamp=1390561121310
DATACENTER=/pr/hello/plc
    TotalNumberOfServers:4
    primary:{0=1, 1=2, 2=1, 3=2, 4=1, 5=2, 6=1, 7=2, 8=1, 9=2, 10=1, 11=2, 12=1, 13=2}
    secondary:{0=0, 1=0, 2=0, 3=1, 4=0, 5=0, 6=0, 7=1, 8=0, 9=0, 10=0, 11=1, 12=0, 13=0}
    hosttomachine:{3=plcdbx1115.plc.domain.com, 2=plcdbx1114.plc.domain.com, 1=plcdbx1113.plc.domain.com, 4=plcdbx1116.plc.domain.com}
DATACENTER=/pr/hello/pty
    TotalNumberOfServers:2
    primary:{0=1, 1=2, 2=1, 3=2, 4=1, 5=2, 6=1, 7=2, 8=1, 9=2, 10=1, 11=2, 12=1, 13=2, 14=1}
    secondary:{0=0, 1=0, 2=0, 3=1, 4=0, 5=0, 6=0, 7=1, 8=0, 9=0, 10=0, 11=1, 12=0, 13=0, 14=0}
    hosttomachine:{1=ptydbx1145.pty.domain.com, 4=ptydbx1148.pty.domain.com}

我无法理解如何以JSON格式表示上述数据。这是我尝试过但不确定这是否正确?

{
    "haschanged": "true",
    "version": "1",
    "timestamp": "1390561121310",
    "/pr/hello/plc":
        {
        // not sure what should I do next?          
        },
    "/pr/hello/pty":
        {
        // not sure what should I do next?          
        }
}

2 个答案:

答案 0 :(得分:0)

你可以这样使用;

使用作为键值对

修改:我在 DATACENTER 键中只放了一个项目。你可以自己做另一件事

{
   "haschanged":true,
   "version":1,
   "timestamp":1390561121310,
   "DATACENTER":[
      {
         "name":"/pr/hello/plc",
         "TotalNumberOfServers":4,
         "primary":[
            {
               "0":"1"
            },
            {
               "1":"2"
            },
            {
               "2":"1"
            },
            {
               "3":"2"
            },
            {
               "4":"1"
            },
            {
               "5":"2"
            },
            {
               "6":"1"
            },
            {
               "7":"2"
            },
            {
               "8":"1"
            },
            {
               "9":"2"
            },
            {
               "10":"1"
            },
            {
               "11":"2"
            },
            {
               "12":"1"
            },
            {
               "13":"2"
            }
         ],
         "secondary":[
            {
               "0":"0"
            },
            {
               "1":"0"
            },
            {
               "2":"0"
            },
            {
               "3":"1"
            },
            {
               "4":"0"
            },
            {
               "5":"0"
            },
            {
               "6":"0"
            },
            {
               "7":"1"
            },
            {
               "8":"0"
            },
            {
               "9":"0"
            },
            {
               "10":"0"
            },
            {
               "11":"1"
            },
            {
               "12":"0"
            },
            {
               "13":"0"
            }
         ],
         "hosttomachine":[
            {
               "3":"plcdbx1115.plc.domain.com"
            },
            {
               "2":"plcdbx1114.plc.domain.com"
            },
            {
               "1":"plcdbx1113.plc.domain.com"
            },
            {
               "4":"plcdbx1116.plc.domain.com"
            }
         ]
      }
   ]
}

答案 1 :(得分:0)

使用数据中心列表,你很高兴。

以下是如何做到的:

{
    "haschanged": "true",
    "version": "1",
    "timestamp": "1390561121310",
    "DATACENTER" = [
        { "name" : "/pr/hello/plc",
          "TotalNumberOfServers": 4,
          "primary": { 0=1, 1=2, 2=1, 3=2, 4=1, 5=2, 6=1, 7=2, 8=1, 9=2, 10=1, 11=2, 12=1, 13=2},
          "secondary": { 0=0, 1=0, 2=0, 3=1, 4=0, 5=0, 6=0, 7=1, 8=0, 9=0, 10=0, 11=1, 12=0, 13=0},
          "hosttomachine": { 3=plcdbx1115.plc.domain.com, 2=plcdbx1114.plc.domain.com, 1=plcdbx1113.plc.domain.com, 4=plcdbx1116.plc.domain.com}"

        },

        { "name": "/pr/hello/pty",
          "TotalNumberOfServers": 2,
          "primary": { 0=1, 1=2, 2=1, 3=2, 4=1, 5=2, 6=1, 7=2, 8=1, 9=2, 10=1, 11=2, 12=1, 13=2, 14=1},
          "secondary": { 0=0, 1=0, 2=0, 3=1, 4=0, 5=0, 6=0, 7=1, 8=0, 9=0, 10=0, 11=1, 12=0, 13=0, 14=0},
          "hosttomachine": { 1=ptydbx1145.pty.domain.com, 4=ptydbx1148.pty.domain.com}
        }
    ]
}