0

とてもシンプルな感じがしますが、未定義以外のものを吐き出すのに苦労しています

関数に文字列を渡すJSONオブジェクトを取得できるようにしたいと思います。これは以下のような機能で可能ですか?

var dashboardData= {
"Dashboard": 
{
    "Total_Downloads":95,
    "Unique_Downloads":30,
    "Campaign_Threshold":1000,
    "Downloads_By_Date":[
        {"Downloads":6,"Date":"9/6/2012 12:00:00 AM"},
        {"Downloads":2,"Date":"9/7/2012 12:00:00 AM"},
        {"Downloads":9,"Date":"9/20/2012 12:00:00 AM"},
        {"Downloads":2,"Date":"9/22/2012 12:00:00 AM"},
        {"Downloads":6,"Date":"9/23/2012 12:00:00 AM"},
        {"Downloads":10,"Date":"9/24/2012 12:00:00 AM"},
        {"Downloads":1,"Date":"9/26/2012 12:00:00 AM"},
        {"Downloads":1,"Date":"9/29/2012 12:00:00 AM"},
        {"Downloads":3,"Date":"10/2/2012 12:00:00 AM"},
        {"Downloads":4,"Date":"10/5/2012 12:00:00 AM"},
        {"Downloads":1,"Date":"10/6/2012 12:00:00 AM"},
        {"Downloads":2,"Date":"10/8/2012 12:00:00 AM"},
        {"Downloads":4,"Date":"10/9/2012 12:00:00 AM"},
        {"Downloads":3,"Date":"10/10/2012 12:00:00 AM"},
        {"Downloads":1,"Date":"10/12/2012 12:00:00 AM"},
        {"Downloads":27,"Date":"10/16/2012 12:00:00 AM"},
        {"Downloads":2,"Date":"10/17/2012 12:00:00 AM"},
        {"Downloads":1,"Date":"10/19/2012 12:00:00 AM"},
        {"Downloads":1,"Date":"10/20/2012 12:00:00 AM"},
        {"Downloads":9,"Date":"10/21/2012 12:00:00 AM"}
    ],
  }
};

        function formatJSON(source, key1, key2) {

            var i, l,
                dest = [],
                row;

            for(i = 0, l = source.length; i < l; i++) {
                row = source[i];      

                 dest.push('[' + row.key1, row.key2 + ']');

            }

            return '[' + dest + ']';

        }

var downloads= formatJSON(dashboardData.Dashboard.Downloads_By_Date,'Downloads','Date');     

var output="<ul>";
output+="<li>" + dashboardData.Dashboard.Total_Downloads + "</li>";

output+="</ul>";
document.getElementById("placeholder").innerHTML=downloads;

</ p>

4

2 に答える 2

0

JSON.stringifyそれ自体には、いくつかの書式設定オプションがあります。たとえば、「4」を 3 番目のパラメーターとして渡すことで、4 つのスペースでインデントできます: Demo at jsfiddle.net

于 2012-10-22T00:22:48.303 に答える
0

キーが表すプロパティではなく、行オブジェクトの key1 および key2 プロパティを要求しています。

dest.push('[' + 行 [キー 1] + ',' + 行 [キー 2] + ']');

于 2012-10-22T00:23:01.910 に答える