json返回数组但没有返回我想要的内容

时间:2015-04-28 15:40:51

标签: php arrays json jsonobject

我是PHP的初学者和我的PHPreturns json数组,但是返回了这个:

[{"ContentID":"1","con_Model":"CAR_NAME","Picture_path":"localhost\/1.jpg"},{"ContentID":"2","con_Model":"CAR_NAME1","Picture_path":"localhost\/1.jpg"}]

我想要这个:

["content": {"ContentID":"1","con_Model":"CAR_NAME","Picture_path":"localhost\/1.jpg"},{"ContentID":"2","con_Model":"CAR_NAME1","Picture_path":"localhost\/1.jpg"}]

我的PHP代码:

<?php
    $array = array();
    require("config.inc.php");
    $query="SELECT Content.ContentID,Content.con_Model,Picture.Picture_path from Content INNER JOIN Picture ON Content.ContentID = Picture.ContentID LIMIT 25;";
        try {
            $stmt = $db->prepare($query);
            $stmt->execute();
            $array = $stmt->fetchAll(PDO::FETCH_ASSOC);
        }
        catch (PDOException $ex) {
            // For testing, you could use a die and message. 
            //die("Failed to run query: " . $ex->getMessage());

            //or just use this use this one to product JSON data:
            $response["success"] = 0;
            $response["message"] = "Database Error1. Please Try Again!";
            die(json_encode($response));

        }
            json_encode($array);
            print(json_encode($array));
?>

2 个答案:

答案 0 :(得分:1)

试试这个

< uses-permission android:name="android.permission.SET_TIME_ZONE"/>
< uses-permission android:name="android.permission.BATTERY_STATS"/>

答案 1 :(得分:0)

您所需格式的问题是,它不是有效的JSON。有效的必须如下所示:

 {<key>:<value>, …}

您可以将密钥放在数组前面。这将是一个有效的JSON。