处理PHP代码和JSON中的引号

时间:2016-02-20 00:41:09

标签: php json highcharts

我在PHP中构建一个复杂的JSON数组结果,方法是对数据库执行多次查询,然后将数据推送到数组中,最后使用JSON_NUMERIC_CHECK对其进行json_encode。我得到所有数字数组干净,没有引号。问题是名称/值对的集合

$series = array();
$series['name'] = 'Trend';
while($row = mysql_fetch_array($result)){
$series['data'][] = 'y: ' . $row['A'] . ',ratio: ' . round($row['A'] / $row['B'] * 100, 0) . '}';
}
$result = array();
array_push($result,$series);
print json_encode($result, JSON_NUMERIC_CHECK);
array_push($result,$category);

具有HighCharts中不可接受的外部引号。这是PHP代码的片段:

{y: 39,ratio: 150},...,{y: 40,ratio: 364}

如何摆脱引号并查看以下内容:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JFrame implements ActionListener {

    Font Ft = new Font("Verdana",Font.BOLD,14);
    JLabel Lb1 = new JLabel("Select an add-on to your burger");
    JCheckBox Egg = new JCheckBox("Egg Php10");
    JCheckBox Cheese = new JCheckBox("Cheese Php10");
    JCheckBox Bacon = new JCheckBox("Bacon Php15");
    JButton UnCheck = new JButton("UnCheck All");
    JButton CheckAll = new JButton("Check All");
    JButton Submit = new JButton("Submit");
    JLabel Lb2 = new JLabel("Total Amount: ");
    JLabel Lb3 = new JLabel("");

    public Test()//Swing Components
    {
        setTitle("Color Settings");
        setSize(300,420);
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setBackground(Color.BLACK);

        Lb1.setFont(Ft);
        Lb1.setBounds(20,30,300,20);
        Lb1.setForeground(Color.YELLOW);

        Egg.setFont(Ft);
        Egg.setBounds(40,65,300,20);
        Egg.setForeground(Color.WHITE);
        Egg.setBackground(Color.BLACK);

        Cheese.setFont(Ft);
        Cheese.setBounds(40,95,300,20);
        Cheese.setForeground(Color.WHITE);
        Cheese.setBackground(Color.BLACK);

        Bacon.setFont(Ft);
        Bacon.setBounds(40,125,300,20);
        Bacon.setForeground(Color.WHITE);
        Bacon.setBackground(Color.BLACK);

        UnCheck.setBounds(20,160,100,20);
        CheckAll.setBounds(150,160,100,20);
        Submit.setBounds(70,200,100,20);

        Lb2.setFont(Ft);
        Lb2.setBounds(20,250,300,20);
        Lb2.setForeground(Color.YELLOW);

        Lb3.setFont(Ft);
        Lb3.setBounds(150,250,300,20);
        Lb3.setForeground(Color.YELLOW);

        add(Lb1);
        add(Egg);
        add(Cheese);
        add(Bacon);
        add(UnCheck);
        add(CheckAll);
        add(Submit);
        add(Lb2);
        add(Lb3);     

        UnCheck.addActionListener(new ActionListener()//First button to have a separate action.
        {
            public void actionPerformed(ActionEvent E)
            {
                Egg.setSelected(false);
                Cheese.setSelected(false);
                Bacon.setSelected(false);
            }
        });
        CheckAll.addActionListener(new ActionListener()//Second button to have a separate action.
        {
            public void actionPerformed(ActionEvent E)
            {
                Egg.setSelected(true);
                Cheese.setSelected(true);
                Bacon.setSelected(true);
            }
        });
        Submit.addActionListener(new ActionListener()//Third button to have a separate action.
        {
            public void actionPerformed(ActionEvent E)
            {
                int price;

                if (Egg.isSelected())
                {
                    price = price + 10;
                }
                if (Cheese.isSelected())
                {
                    price = price +10;
                }
                if (Bacon.isSelected())
                {
                   price = price + 15;
                }
                Lb3.setText("Php" + price);
            }
        });
    }
    public static void main (String[] args)
    {
        Test Fr = new Test();
        Fr.setVisible(true);
    }
}

2 个答案:

答案 0 :(得分:1)

删除此行:

$series['data'][] = 'y: ' . $row['A'] . ',ratio: ' . round($row['A'] / $row['B'] * 100, 0) . '}';

用以下代码替换上面的代码:

$series['data'][] = array("y" => $row['A'],
    "ratio" => round($row['A'] / $row['B'] * 100, 0));

答案 1 :(得分:0)

你的意思是这样吗

while($row = mysql_fetch_array($result)) {
   $series['data'][] = (object)array(
     'y' => $row['A'],
     'ratio' => round($row['A'] / $row['B'] * 100, 0)
   );
 }

然后,你可以

print json_encode($series);