duande8497 2014-10-17 10:37
浏览 45
已采纳

如何在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.

  • 写回答

1条回答 默认 最新

  • douba8048 2014-10-17 10:50
    关注

    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, /*...*/)))

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决