dongtan7351 2016-01-06 07:04
浏览 11
已采纳

更改JSON格式

I am working with drupal 8. I am trying to get the JSON of all nodes of the content type. I got a json as given bellow. But Now I want to change the Following JSON to

[
    {
        "nid": [
            {
                "value": "17"
            }
        ],
        "uuid": [
            {
                "value": "3614e0c8-88d4-4e8d-a732-5089698556d5"
            }
        ],
        "vid": [
            {
                "value": "17"
            }
        ],
        "type": [
            {
                "target_id": "resume_creator"
            }
        ],
        "langcode": [
            {
                "value": "en"
            }
        ],
        "title": [
            {
                "value": "uyi"
            }
        ],
        "uid": [
            {
                "target_id": "1"
            }
        ],
        "status": [
            {
                "value": "1"
            }
        ],
        "created": [
            {
                "value": "1452060690"
            }
        ],
        "changed": [
            {
                "value": "1452060709"
            }
        ],
        "promote": [
            {
                "value": "1"
            }
        ],
        "sticky": [
            {
                "value": "0"
            }
        ],
        "revision_timestamp": [
            {
                "value": "1452060709"
            }
        ],
        "revision_uid": [
            {
                "target_id": "1"
            }
        ],
        "revision_log": [],
        "revision_translation_affected": [
            {
                "value": "1"
            }
        ],
        "default_langcode": [
            {
                "value": "1"
            }
        ],
        "path": [],
        "field_communication_address": [
            {
                "value": "rtyrtytr
uu;
sdgfdh"
            }
        ],
        "field_education": [
            {
                "value": "ytutyuii"
            }
        ],
        "field_emails": [
            {
                "value": "gtf@fgfg.com"
            }
        ],
        "field_experiece": [
            {
                "value": "fghtutyu"
            }
        ],
        "field_name": [
            {
                "value": "ytt"
            }
        ]
    }
]

to a format of

[
    {
        "nid":"17",
        "uuid":"3614e0c8-88d4-4e8d-a732-5089698556d5",
        "vid": "17",
        "type":"resume_creator",
        "langcode":"en",
        "title":"uyi",            
        "uid":"1",
        "status":"1",   
        "created":"1452060690",
        "changed":"1452060709",
        "promote":"1",
        "sticky":"0",
        "revision_timestamp":"1452060709",
        "revision_uid":"1",
        "revision_log": [],
        "path":[],
        "field_communication_address":"rtyrtytr
uu;
sdgfdh",
        "field_education":"ytutyuii",
        "field_emails":"gtf@fgfg.com",
        "field_experiece":"fghtutyu",
        "field_name":"ytt"
    }
]

using php. Then only I can manage a form angular js. Thanks in advance

  • 写回答

2条回答 默认 最新

  • dongxin2734 2016-01-06 07:21
    关注

    Try this

     $json = '{
                "nid": [
                    {
                        "value": "17"
                    }
                ],
                "uuid": [
                    {
                        "value": "3614e0c8-88d4-4e8d-a732-5089698556d5"
                    }
                ],
                "vid": [
                    {
                        "value": "17"
                    }
                ],
                "type": [
                    {
                        "target_id": "resume_creator"
                    }
                ],
                "langcode": [
                    {
                        "value": "en"
                    }
                ],
                "title": [
                    {
                        "value": "uyi"
                    }
                ],
                "uid": [
                    {
                        "target_id": "1"
                    }
                ],
                "status": [
                    {
                        "value": "1"
                    }
                ],
                "created": [
                    {
                        "value": "1452060690"
                    }
                ],
                "changed": [
                    {
                        "value": "1452060709"
                    }
                ],
                "promote": [
                    {
                        "value": "1"
                    }
                ],
                "sticky": [
                    {
                        "value": "0"
                    }
                ],
                "revision_timestamp": [
                    {
                        "value": "1452060709"
                    }
                ],
                "revision_uid": [
                    {
                        "target_id": "1"
                    }
                ],
                "revision_log": [],
                "revision_translation_affected": [
                    {
                        "value": "1"
                    }
                ],
                "default_langcode": [
                    {
                        "value": "1"
                    }
                ],
                "path": [],
                "field_communication_address": [
                    {
                        "value": "rtyrtytr
    uu;
    sdgfdh"
                    }
                ],
                "field_education": [
                    {
                        "value": "ytutyuii"
                    }
                ],
                "field_emails": [
                    {
                        "value": "gtf@fgfg.com"
                    }
                ],
                "field_experiece": [
                    {
                        "value": "fghtutyu"
                    }
                ],
                "field_name": [
                    {
                        "value": "ytt"
                    }
                ]
            }';
    
        $json = json_decode($json,true);
        foreach ($json as $key => $value){
    
            if(isset($json[$key][0]['value'])){
                $json[$key] = $json[$key][0]['value'];
            }
    
            if(isset($json[$key][0]['target_id'])){
                $json[$key] = $json[$key][0]['target_id'];
            }
           // $json[$key] = $json[$key][0]['value']; 
        }
        $json = json_encode($json);
    
        print_r($json);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧