以特定格式创建json对象

时间:2013-08-17 07:31:16

标签: php javascript

我想写一个脚本,通过它我可以生成json,如下面的格式。

[
        [ "20 aug 2013",                                      
            [
                {lbl1: "Titanium Card",lbl2: "$2000",lbl3: "$400",img1234:"checkboxwhite.png"}, 
                {lbl1: "Gold Card",lbl2: "$1000",lbl3: "$500",img1234:"checkboxwhite.png"}, 
                {lbl1: "Silver Card",lbl2: "$800",lbl3: "$600",img1234:"checkboxwhite.png"}
            ]
        ],

        [ "22 aug 2013",                                               
            [
                {lbl1:"Savings 1", lbl2: "$400",lbl3:"N/A",img1234:"checkboxwhite.png"},
                {lbl1: "Savings 2", lbl2: "$400",lbl3: "N/A",img1234:"checkboxwhite.png"}

            ]
        ],
        [ "24 aug 2012",
            [                                                     
                {lbl1: "Checking 1",lbl2: "N/A",lbl3: "$400",img1234:"checkboxwhite.png"},
                {lbl1: "Checking 2",lbl2: "N/A",lbl3: "$400",img1234:"checkboxwhite.png"}
            ]
        ]     
    ];

Json对象:

var jsonObj= {"20th Aug 2013":[{"agenda":"Eventbuilder program session for next events","endtime":"20th Aug 2013 16:00 pm","starttime":"20th Aug 2013 09:00 am","status":"1","location":"Sardar Patel Stadium, Ahmedabad, Gujarat","speaker":"Salim Kg","parent":"0","type":"Entertainment","lang":"72.5644771","id":"1","title":"Eventbuilder program session","isAttend":0,"app_id":"2","speaker_id":"1","lat":"23.0419169","floor_plan":"floorplan_2.png","handoutfile":"test1.pdf"}],"18th Aug 2013":[{"agenda":"entertainment weekly events","endtime":"18th Aug 2013 16:00 pm","starttime":"18th Aug 2013 09:00 am","status":"1","location":"GMDC Ground Counter1,Ahmedabad","speaker":"Salim Kg","parent":"0","type":"Entertainment","lang":"72.5669487","id":"2","title":"Eventbuilder Team meeting ","isAttend":1,"app_id":"2","speaker_id":"1","lat":"23.0400935","floor_plan":"floorplan_3.png","handoutfile":""}],"17th Aug 2013":[{"agenda":"entertainment weekly events","endtime":"17th Aug 2013 16:00 pm","starttime":"17th Aug 2013 09:00 am","status":"1","location":"GMDC Ground Counter2,Ahmedabad","speaker":"Salim Kg","parent":"0","type":"Entertainment","lang":"72.5644771","id":"3","title":"8th International Workshop on Commercial Law","isAttend":1,"app_id":"2","speaker_id":"1","lat":"23.0419169","floor_plan":"","handoutfile":"sc3.pdf"}],"16th Aug 2013":[{"agenda":"entertainment weekly events","endtime":"16th Aug 2013 16:00 pm","starttime":"16th Aug 2013 09:00 am","status":"1","location":"Sardar Patel Stadium, Ahmedabad, Gujarat","speaker":"Salim Kg","parent":"0","type":"Entertainment","lang":"72.5644771","id":"4","title":"Team building ","isAttend":0,"app_id":"2","speaker_id":"1","lat":"23.0419169","floor_plan":"","handoutfile":"sc4.pdf"}],"19th Aug 2013":[{"agenda":"entertainment weekly events","endtime":"19th Aug 2013 16:00 pm","starttime":"19th Aug 2013 09:00 am","status":"1","location":"GMDC Ground Counter1,Ahmedabad","speaker":"Salim Kg","parent":"0","type":"Entertainment","lang":"72.5669487","id":"5","title":"Ceritification course","isAttend":1,"app_id":"2","speaker_id":"1","lat":"23.0400935","floor_plan":"","handoutfile":""}],"21st Aug 2013":[{"agenda":"entertainment weekly events","endtime":"21st Aug 2013 16:00 pm","starttime":"21st Aug 2013 09:00 am","status":"1","location":"GMDC Ground Counter2,Ahmedabad","speaker":"chirag ch","parent":"0","type":"Entertainment","lang":"72.5644771","id":"6","title":"Networking Breakfast","isAttend":0,"app_id":"2","speaker_id":"2","lat":"23.0419169","floor_plan":"","handoutfile":""}]};

我的代码但没有成功

var keyArr =[];
    for (var key in jsonObj) {
        keyArr.push(key);

    }
    var i =0;
    for(i=0;i<keyArr.length; i++){
        //console.log(jsonObj[keyArr[i]]);
        //console.log("main obj=="+jsonObj[keyArr[0]]);
        keyArr.i =[];
        for(x in jsonObj[keyArr[i]]){
            keyArr.i.push({
                lbl1:jsonObj[keyArr[i]][x]['title'],
                lbl2:jsonObj[keyArr[i]][x]['agenda'],
                template:"hboxEventSection"
            });
        }

    }
    console.log(keyArr);

请帮忙

提前感谢。

1 个答案:

答案 0 :(得分:0)

没有内置的方法可以做到这一点。 PHP的JSON函数旨在尽可能地压缩数据以进行传输或存储。它并不意味着是人类可读的格式,因此消除了不必要的空白区域。

您必须编写自己的代码来循环遍历数组并手动生成示例中的格式。