0

i'm developing an android project which i need to create a JSON message in my php server and send it to the android devices.

i wrote the following code in php

$event_array = array();
// fill the event_array with some data
print json_encode(array('event_array' => $event_array));

but the result is like

 {
    "event_array": {
        "id_1": {
            "name": "name",
            "logo_address": "logo_address",
            "title": "title",
            "time": null,
            "address": null,
            "address_location": null,
            "explain": null,
            "type": null,
            "id": "id_1",
            "number_of_users": null
        },
        "id_2": {
            "name": "name2",
            "logo_address": null,
            "title": null,
            "time": null,
            "address": null,
            "address_location": null,
            "explain": null,
            "type": null,
            "id": "id_2",
            "number_of_users": null
        }
    }
}

and it's not a json array and i get exception in my android code which simply is

JSONObject jObject = new JSONObject(res);
JSONArray jArray = jObject.getJSONArray("event_array");

what's wrong?

thanks for any help

3
  • Use json.parser.online.fr/beta to know types of JSON Commented Jul 14, 2014 at 7:01
  • Try json_encode($event_array)); Commented Jul 14, 2014 at 7:04
  • @Pratik Butani: i try this, but it's not print like a jsonarray and it doesn't have any name :( Commented Jul 14, 2014 at 7:07

1 Answer 1

1

{ means object, [ means array.

In your case its two objects.

JSONObject jObject = new JSONObject(res);

is correct and contains another object called event_array.

JSONObject jsonEvents = new JSONObject(jObject.getString("event_array"));

while jsonEvents now holds

jsonEvents.getString("id_1");

which is another jsonObject.

If you want to output as array, use array again.

As written in http://de3.php.net/json_encode it must be something like this to get an array

echo json_encode(array(array('event_array' => $event_array)));

So it means it should be like this to match your case.

 echo json_encode(
    array(
        'event_array' =>
        array(
            array("id_1" => array('name' => 'name', 'logo_...' => '...')),
            array("id_2" => array('name' => '....', 'logo_....' => '....'))
        )
 ));

to read this in Java its more likely

 JSONObject json = new JSONObject(data);
 JSONArray jsonArr = json.getString('event_array');

 for (int i = 0; i <= jsonArr.length(); i++) {
    JSONObject jsonEventData = jsonArr.getJsonObject(i);
 }
Sign up to request clarification or add additional context in comments.

4 Comments

i know this! but i want to have a jsonarray and not a jsonobject! i try "echo json_encode(array('event_array' => $event_array)), "\n";" but the result was same as before :(
i try this too, it's change to [{"event_array":{"id_1":{"name":"name","logo_address":"logo_address","title":"title","time":null,"address":null,"address_location":null,"explain":null,"type":null,"id":"id_1","number_of_users":null},"id_2":{"name":"name2","logo_address":null,"title":null,"time":null,"address":null,"address_location":null,"explain":null,"type":null,"id":"id_2","number_of_users":null}}}] but the exception still exists. by the way thanks a lot for your time :)
so, its not wrong. You can see how it works. Ill update the answer with an example for your case.
nop! :((( it doesn't work too :( you can see the result here: foroutannezhad.com/temp/events.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.