-3

我想编写一个脚本,通过它我可以生成如下格式的 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);

请帮忙

提前致谢。

4

1 回答 1

0

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

您将不得不编写自己的代码来遍历您的数组并手动从您的示例中生成格式。

于 2013-08-17T08:05:36.150 回答