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条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图