解码json并在表格中显示

时间:2016-02-01 21:56:22

标签: php jquery html

我有一个HotUKDeals API,当使用下面的URL时,我会给我一些JSON。 我试图使用PHP在表中显示它,但如果我尝试使用循环来通过JSON我会得到错误。

如果我尝试解码我​​得到"类stdClass的对象无法转换为字符串"

到目前为止,我有这个我没有得到json并将其存储在变量中。

我也有一些JS将通过JSON并将其放在一个表中,但我很难将php变量中的JSON转换为JS。

Needed to remove code for work reasons !

1 个答案:

答案 0 :(得分:2)

要将JSON添加到您的jQuery代码中,您必须使用AJAX:

PHP文件 -

String s;
if (c != null && c.moveToFirst()) 

        while (c.moveToNext()) 
                 s = c.getString(c.getColumnIndexOrThrow("string"));

c.close();

jQuery AJAX -

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class MatrixMultiplication {
    public static class Matrix {
        int[][] container;
        int rows;
        int cols;

        public Matrix(int rows, int cols) {
            this.rows = rows;
            this.cols = cols;
            container = new int[rows][cols];
        }
    }

    public static class MultiplyThread implements Runnable {
        private int row;
        private int col;
        Matrix a;
        Matrix b;
        Matrix c;

        public MultiplyThread(Matrix a, Matrix b, Matrix c, int row, int col) {
            this.row = row;
            this.col = col;
            this.a = a;
            this.b = b;
            this.c = c;
        }

        @Override
        public void run() {
            int sum = 0;
            for (int i = 0; i < a.cols; i++) // or b.rows
                sum += a.container[row][i] * b.container[i][col];
            c.container[row][col] = sum;
        }
    }

    public static Matrix multiplyMatrices(Matrix a, Matrix b, int threads) throws InterruptedException {
        if (a.cols != b.rows)
            return null;
        int rows = a.rows;
        int cols = b.cols;
        Matrix ret = new Matrix(rows, cols);
        ExecutorService executor = Executors.newFixedThreadPool(threads);
        int i = 0, j = 0;
        for (i = 0; i < rows; i++)
            for (j = 0; j < cols; j++)
                executor.submit(new MultiplyThread(a, b, ret, i, j));
        executor.shutdown();
        executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        return ret;
    }

    public static void main(String[] args) throws InterruptedException {
        int[][] mat = new int[][] { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 } };
        Matrix a = new Matrix(4, 4);
        a.container = mat;
        Matrix b = new Matrix(4, 4);
        b.container = mat;

        int numProcessors = Runtime.getRuntime().availableProcessors();
        System.out.println(numProcessors);
        for (int i = 10; i >= 1; i--) {
            long time = System.currentTimeMillis();
            for (int j = 0; j < 10000; j++)
                multiplyMatrices(a, b, i);
            time = System.currentTimeMillis() - time;
            System.out.println("number of threads: " + i + ", and the time: " + time);
        }
    }

}

注意:返回的JSON具有复杂的构造,因此您必须注意在循环期间如何提取然后放入表中。