如何制作像这样的JSON输出?

时间:2016-03-30 06:23:58

标签: php json

我想要的输出

 private Byte[] myChart()
    {
        var query = from o in db.charttest
                    group o by new { o.Date, o.Value }
             into g
                    select new
                    {
                        Value = g.Key.Value,
                        Date = g.Key.Date,
                        Number = g.Count()
                    };
        var chart = new Chart
        {
            Width = 700,
            Height = 450,
            RenderType = RenderType.ImageTag,
            AntiAliasing = AntiAliasingStyles.All,
            TextAntiAliasingQuality = TextAntiAliasingQuality.High
        };
        chart.Titles.Add("Summary");
        chart.Titles[0].Font = new Font("Arial", 16f);
        chart.ChartAreas.Add("");
        chart.ChartAreas[0].AxisX.Title = "Date";
        chart.ChartAreas[0].AxisY.Title = "Value";
        chart.ChartAreas[0].AxisX.TitleFont = new Font("Arial", 12f);
        chart.ChartAreas[0].AxisY.TitleFont = new Font("Arial", 12f);
        chart.ChartAreas[0].AxisX.LabelStyle.Font = new Font("Arial", 10f);
        chart.ChartAreas[0].AxisX.LabelStyle.Angle = -90;
        chart.ChartAreas[0].BackColor = Color.White;
        chart.Series.Add("");
        chart.Series[0].ChartType = SeriesChartType.Column;
        foreach (var q in query)
        {
            var Name = q.Value;
            var Day = q.Date;
             chart.Series[0].Points.AddXY(Day, Name);
        }
        using (var chartimage = new MemoryStream())
        {
            chart.SaveImage(chartimage, ChartImageFormat.Png);
            return chartimage.GetBuffer();
        }
    }

我想要的输出结束

我当前的JSON输出

{"rowcount":4
[{"provider_id":"1","provider_name":"Crecent Computers","sub_name":["Hardware","Software","Networks"]}],[{"provider_id":"4","provider_name":"Testing Co. LLC","sub_name":["Hardware","Software","Networks"]}],[{"provider_id":"41","provider_name":"Itiology","sub_name":["Hardware","Software","Networks","All IT Services"]}],[{"provider_id":"42","provider_name":"ITiology","sub_name":["Hardware","Software","Networks","All IT Services","Website Design "]}]}

JSON输出结束

PHP代码

{"rowcount":4,"0":[{"provider_id":"1","provider_name":"Crecent Computers","sub_name":["Hardware","Software","Networks"]}],"1":[{"provider_id":"4","provider_name":"Testing Co. LLC","sub_name":["Hardware","Software","Networks"]}],"2":[{"provider_id":"41","provider_name":"Itiology","sub_name":["Hardware","Software","Networks","All IT Services"]}],"3":[{"provider_id":"42","provider_name":"ITiology","sub_name":["Hardware","Software","Networks","All IT Services","Website Design "]}]}

PHP代码结束

2 个答案:

答案 0 :(得分:0)

你想要的输出json是一个无效的json。你可以在http://jsonviewer.stack.hu中检查这个json是否有效。 json或xml有自己的模式。

答案 1 :(得分:0)

所以我发现我想要的输出无效。对不起,我是PHP的新手。

我删除了这一行

$data_points["rowcount"] = $rowcount;

并且做了这个

$data_points[][] = [
        'provider_id' => $row['provider_id'],
        'provider_name' => $row['provider_name'],
        'sub_name' => $subcatArray
    ];

进入这个

$data_points["Rows"][] = [
        'provider_id' => $row['provider_id'],
        'provider_name' => $row['provider_name'],
        'sub_name' => $subcatArray
    ];

我的输出非常接近我所需要的。谢谢你们!