如何从类中正确格式化json编码?

时间:2013-08-28 19:07:31

标签: php class charts json

我无法按照我想要/需要的方式正确格式化我的json。我正在使用open-flash-charts,我的图表不接受json我给它... 一切都是在开放式闪存图用作数据文件的php内部完成的。我只是回应我的json,这可能是问题还是json格式?

class Charts {
public $title = ["title"=>['text' => "ae", 'style' => "za"]];
public $y_legend = ["y_legend"=>['text' => "", 'style' => ""]];
public $x_axis = ["x_axis"=>['stroke' => '1', 'tick_height' => '10', 'colour' => "#d000d0", 'grid_colour' => "#00ff00", 'labels' => [] ]];
public $y_axis = ["y_axis"=>['stroke' => '1', 'tick_length' => '10', 'colour' => "#d000d0", 'grid_colour' => "#00ff00", 'offset' => '0', 'max' => '20']];
public $bar = ["elements"=>['type' => "bar", 'alpha' => '0.5', 'colour' => '#9933CC', 'text' => "", 'font-size' => '10', 'values' => [] ]];
public $pie =  ["elements" => ['type' => "pie", 'start_angle' => '180', 'colours' => ["#d01f3c","#356aa0","#C79810","#73880A","#D15600","#6BBA70"], 'alpha' => '0.6', 'stroke' => '2', 'animate' => '0', 'values' => [] ]];

}

$a = new Charts();  
$a->title['title']['text'] = "Dates";   
$a->title['title']['style'] = "{font-size: 20px; color:#0000ff; font-family: Verdana; text-align: center;}";
$a->y_legend['y_legend']['text'] = "Khash/s";
$a->y_legend['y_legend']['style'] = "{font-size: 10px; color:#0000ff; font-family: Verdana; text-align: center;}";
$a->x_axis['x_axis']['labels'] = get_val_date($poolstale_enc_json); // Function returns an array of values
$a->bar['elements']['text'] = "barText";
$a->bar['elements']['values'] = get_val_value($poolstale_enc_json);// Function returns an array of values

$all_data = $a->title;
$all_data += $a->y_legend;
$all_data += $a->x_axis;
$all_data += $a->bar;

echo json_encode($all_data);

这给出了输出:

{
   "title":{
      "text":"Dates",
      "style":"{font-size: 20px; color:#0000ff; font-family: Verdana; text-align: center;}"
   },
   "y_legend":{
      "text":"Khash\/s",
      "style":"{font-size: 10px; color:#0000ff; font-family: Verdana; text-align: center;}"
   },
   "x_axis":{
      "stroke":"1",
      "tick_height":"10",
      "colour":"#d000d0",
      "grid_colour":"#00ff00",
      "labels":[
         "2013-08-25"
      ]
   },
   "elements":{
      "type":"bar",
      "alpha":"0.5",
      "colour":"#9933CC",
      "text":"barText",
      "font-size":"10",
      "values":[
         "2.96"
      ]
   }
}

但我需要格式正确,因为我的图表不会接受这个json。他们的网站正确的示例:

{
 "title":{
 "text":"Many data lines",
 "style":"{font-size: 30px;}"
 },

 "y_legend":{
 "text":"Open Flash Chart",
 "style":"{font-size: 12px; color:#736AFF;}"
 },

 "elements":[
  {
   "type":      "line",
   "colour":    "#9933CC",
   "text":      "Page views",
   "width":     2,
   "font-size": 10,
   "dot-size":  6,
   "values" :   [15,18,19,14,17,18,15,18,17]
  },
  {
   "type":      "line_dot",
   "colour":    "#CC3399",
   "width":     2,
   "text":      "Downloads",
   "font-size": 10,
   "dot-size":  5,
   "values" :   [10,12,14,9,12,13,10,13,12]
  },
  {
   "type":      "line_hollow",
   "colour":    "#80a033",
   "width":     2,
   "text":      "Bounces",
   "font-size": 10,
   "dot-size":  6,
   "values" :   [5,7,9,7,4,6,1,2,5]
  }
 ],

 "y_axis":{
  "max":   20
 },

 "x_axis":{
  "steps": 2,
  "labels":  ["January","February","March","April","May","June","July","August","September"]
 }
}

0 个答案:

没有答案