简体   繁体   中英

How to create a JSON array in php?

I am trying to create a simple android application that takes data from a database and displays it in a list format on the android screen. I made a php script that queries the database and returns a json object. I convert the json object into json array and extract the relevant data for display. But I am getting this error "JSONException: type org.json.JSONObject cannot be converted to JSONArray".

Following is my php script -

       // response Array

        $response = array("tag" => $tag, "success" => 0, "error" => 0);
        $username = $_POST['username'];
        $events = $db->viewAttendingEvent($username);

        if ($events) {
        $response["success"] = 1;
        $response["event"]["owner"] = $events["owner"];
        $response["event"]["friendsnames"] = $events["friendsnames"];
        $response["event"]["vote"] = $events["vote"];
        $response["event"]["accepted"] = $events["accepted"];
        $response["event"]["eventname"] = $events["eventname"];
        $response["event"]["eventnumber"] = $events["eventnumber"];
        $response["event"]["created_at"] = $events["created_at"];
        echo json_encode($response);

This is the json which I receive back :

{
    "tag": "view_invitations",
    "success": 1,
    "error": 0,
    "event": {
        "owner": "jkkkkoopp",
        "friendsnames": "don",
        "vote": "0",
        "accepted": "f",
        "eventname": "yyy",
        "eventnumber": "11",
        "created_at": "2014-05-29 22:27:31.843528"
    }
}

I am trying to extract 'event' from this json object, which is not an array. it should be

{
    "event": [
        {
            "owner": "jkkkkoopp",
            "friendsnames": "don",
            "vote": "0",
            "accepted": "f",
            "eventname": "yyy",
            "eventnumber": "11",
            "created_at": "2014-05-2922: 27: 31.843528"
        }
    ]
}

Can someone help me how to make this a valid jsonArray ? Thanks

If you're looking to get a JavaScript 'Array' (from which I mean an Object with nothing but integer keys) then you need to only have integer keys in your PHP Array.

This article is a pretty good resource and explains some of the differences between arrays and objects in javascript. The relevant quote here comes from the What Arrays Are section (emphasis mine):

Javascript arrays are a type of object used for storing multiple values in a single variable. Each value gets numeric index and may be any data type.

No it should not be what you proposed it should be. If that were the case you would have to have your php be this:

$response["event"][0]["owner"] = $events["owner"];
$response["event"][0]["friendsnames"] = $events["friendsnames"];
$response["event"][0]["vote"] = $events["vote"];
$response["event"][0]["accepted"] = $events["accepted"];
$response["event"][0]["eventname"] = $events["eventname"];
$response["event"][0]["eventnumber"] = $events["eventnumber"];
$response["event"][0]["created_at"] = $events["created_at"];

The way you have it now is event is an associative array so it converts it to an object. You are expecting that event = an array of objects. So you need to either change your php code to make event be an array of objects (as demonstrated above) or you need to modify your expectations to have event = an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM