如何创建从另一个json文件读取的json对象

时间:2015-12-16 04:12:22

标签: javascript jquery json

我有像这样的json

{
"returnCode" : "200",
"message"     : "",
"Payload":[
  {
   "id" : "A",
   "series" : "Q1",
   "value" : "100",
  },{
   "id" : "A",
   "series" : "Q2",
   "value" : "110",
  },{
   "id" : "B",
   "series" : "Q1",
   "value" : "130",
  },{
   "id" : "B",
   "series" : "Q2",
   "value" : "150",
  }
 ]
}

我正在阅读

$.getJSON("file.json",function(data){
     $.each(data.Payload, function(i,v){

     }    
})

我也可以迭代。但是从json读取我需要创建另一个将使用的json对象是融合图表。我需要创建的json就像我给出的enter image description here

截图

Plz帮我动态创建..

2 个答案:

答案 0 :(得分:0)

你的代码应该是这样的 另外你的json是不同的,不能完全填充图表所有参数。 你必须包括" 系列名称"即数据值列表。这里只有一个包含值的列表。

var catagory = [];
var datasetData = [];

//set list
if (response.returnCode == 200) {
    if (response.Payload.length > 0) {
        $(respomse.Payload).each(function (i, item) {
            catagory.push({
                "label": item.series
            });

            datasetData.push({
                "value": item.value
            });
        });
    }
}

你的Json对象应该是

var obj = {
    type: 'mscolumn3dlinedy',
    renderAt: 'chart-container',
    width: '550',
    height: '350',
    dataFormat: 'json',
    dataSource: {
        "chart": {
            "caption": "Product-wise Quarterly Revenue vs. Profit %",
            "subCaption": "Harry's SuperMart - Last Year",
            "xAxisname": "Quarter",
            "pYAxisName": "Sales",
            "sYAxisName": "Profit %",
            "numberPrefix": "$",
            "sNumberSuffix": "%",
            "sYAxisMaxValue": "25",
            "paletteColors": "#0075c2,#1aaf5d,#f2c500",
            "bgColor": "#ffffff",                
            "showBorder": "0",
            "showCanvasBorder": "0",
            "usePlotGradientColor": "0",
            "plotBorderAlpha": "10",
            "legendBorderAlpha": "0",
            "legendBgAlpha": "0",
            "legendShadow": "0",
            "showHoverEffect":"1",
            "valueFontColor": "#ffffff",
            "rotateValues": "1",
            "placeValuesInside": "1",
            "divlineColor": "#999999",
            "divLineIsDashed": "1",
            "divLineDashLen": "1",
            "divLineGapLen": "1",
            "canvasBgColor": "#ffffff",
            "captionFontSize": "14",
            "subcaptionFontSize": "14",
            "subcaptionFontBold": "0"
        },
        "categories": [
            {
                "category": catagory
            }
        ],
        "dataset": [
            {
                "seriesname": "Food Products",
                "data": datasetData
            },
            {
                "seriesname": "Non-Food Products",
                "data": datasetData
            },
            {
                "seriesname": "Profit %",
                "renderAs": "line",
                "parentYAxis": "S",
                "showValues": "0",
                "data": datasetData
                ]
            }
        ]
    }
}

将对象应用于绑定图表

var revenueChart = new FusionCharts(obj);

答案 1 :(得分:0)

基本上,根据我的理解,您正在尝试格式化getJSON调用的结果。最简单的方法是按照您需要的方式组织“file.json”中的Payload对象。但是,如果不可能,则必须记住您知道data返回的getJSON对象的格式和属性。只需复制你需要的那些。此代码将代替您的回调function (data)

$.getJSON("file.json",function(data){
    // This object will store our properly formatted data, if you need 
    // access to it outside of this function, declare it outside of the callback
    var formatted_data = {
        "categories" : [],
        "dataset": []
    };

    // Used for efficiency to check if for duplicates
    var foundCategories = [];
    var foundSeries = [];

    // Loop over the result
    $.each(data.Payload, function(index, value) {
        // If we have seen this id already, there's nothing more to do
        var id = value["id"];
        if (foundCategories.indexOf(id) === -1) {
            // We have not seen this id before, store it and keep track of it
            foundCategories.push(id);
            formatted_data["categories"].push({ "category": { "label": id } });
        }

        // Check if we have seen this id before
        var series = value["series"];
        var indexOfSeries = foundSeries.indexOf(series);
        if (indexOfSeries === -1) {
            // We have not seen this series before, store it and keep track of it
            foundSeries.push(series);
            formatted_data["dataset"].push({
                "seriesname": series,
                "data": [{ "value": value["value"] }]
            });
        } else {
            // Add the data to the correct existing series
            formatted_data["dataset"][indexOfSeries]["data"].push({ "value":    value["value"] });
        }
    });  
});

再一次,如果你能让你的服务器为你格式化JSON会更容易,所以你必须在前端做最少的工作。