如何在json中删除元素数组“[”&“]”?

问题描述:

I already possessed variable JSON encoded, then there are elements in the json array like "[" and "]". the question is? how to remove the element.

This is an example of the json before changing

[{
    "form": {
        "user_id": "1",
        "form_id": 26,
        "form_name": "KireniuS",
        "form_description": "afafasf",
        "form_json": [{
            "id": 72043,
            "name": "EditText",
            "input_type": "text",
            "hint": "",
            "options": ""
        }, {
            "id": "409289",
            "name": "Ini Spinner",
            "input_type": "spinner",
            "hint": "ini saya ro",
            "options": "Saya, dan Dia"
        }],
        "created_at": "17-10-2014_16:49",
        "created_by": "adminweb",
        "last_updated_at": "17-10-2014_16:49"
    }
}]

PHP:

$jadi_json = array();
        foreach($model as $row)
        {
            $json_converter = json_decode($row->json_form);


            $saya = array(
                "form"=>array(
                    "user_id"=>$row->id_user,
                    "form_id"=>$row->id_form,
                    "form_name"=>$row->form_name,
                    "form_description"=>$row->form_description,
                    "form_json"=>$json_converter->form_json,
                    "created_at"=>$row->created_at,
                    "created_by"=>$row->created_by,
                    "last_updated_at"=>$row->last_updated_at
                ));
            array_push($jadi_json, $saya);
        }
        header('Content-Type: application/json');
        echo json_encode($jadi_json);

json format desired

{
    "form": {
        "user_id": "1",
            "form_id": 26,
            "form_name": "KireniuS",
            "form_description": "afafasf",
            "form_json": [{
            "id": 72043,
                "name": "EditText",
                "input_type": "text",
                "hint": "",
                "options": ""
        }, {
            "id": "409289",
                "name": "Ini Spinner",
                "input_type": "spinner",
                "hint": "ini saya ro",
                "options": "Saya, dan Dia"
        }],
            "created_at": "17-10-2014_16:49",
            "created_by": "adminweb",
            "last_updated_at": "17-10-2014_16:49"
    }
}

element arrays "[" and "]" are omitted only superficially.

First of all, your JSON is in list of form objects. To parse JSON, use json_decode($json) or CJSON::decode($json). You will have array like this:

 array(
     0 => array('form' => array(...)),
     // if there is any more `form` elements:
     1 => array('form' => array(...)),
 );

To get first element from array, use $myArray[0] or current($myArray) (if you don't know what key will be first).

In the end:

$myArray = CJSON::decode($row->json_form);
$saya = current($myArray);
$secondSaya = next($myArray);
$lastSaya = end($myArray);

If you can controll from where your JSON comes, try to change it, so it comes as single entry and not as array. (E.g. CJSON::encode(array('form' => array('user_id' => 1, /*...*/)))