0

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

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
user2709885
  • 413
  • 2
  • 8
  • 16
  • Why do you think it isn't a valid json array? It made `$response['event']` an object because you have keys associated with the values. – Pitchinnate May 30 '14 at 14:51
  • [Answered Here](http://stackoverflow.com/questions/7332611/how-do-i-extract-value-from-json) Look at the chosen answer it should solve your problems. – Bioto May 30 '14 at 14:52
  • What do you mean with `This is the json which I receive back`? Is that on the client? You should tell us first what the output of `json_encode($response);` is on the server. – greenapps May 30 '14 at 14:52
  • this is the output of json_encode($response) : {"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"}} – user2709885 May 30 '14 at 15:08
  • But json array required is : {"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"}]} – user2709885 May 30 '14 at 15:10

2 Answers2

0

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.

Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
0

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.

Pitchinnate
  • 7,517
  • 1
  • 20
  • 37
  • I changed my php script to the way you have written but I am still getting this JSON on my browser : {"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 not getting a json array – user2709885 May 30 '14 at 15:06
  • Are you sure it isn't caching or anything? Try hitting the api call from just your browser and see if you get the correct response. – Pitchinnate May 30 '14 at 15:12
  • Still getting the same response – user2709885 May 30 '14 at 15:22
  • You have `if ($events) {` what happens if `$events` is empty or null? Also are you setting your headers that specify it is a json being returned? – Pitchinnate May 30 '14 at 15:26
  • If events is empty it simple prints "invalid". But I am not setting any headers. Which header do I have to set – user2709885 May 30 '14 at 15:28
  • http://stackoverflow.com/questions/4064444/returning-json-from-a-php-script/4064468#4064468 – Pitchinnate May 30 '14 at 15:33