更改JSON格式

时间:2016-01-06 07:04:36

标签: php json

我正在使用drupal 8.我正在尝试获取内容类型的所有节点的JSON。我得到了一个json,如下所示。但现在我想将以下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\r\nuu;\r\nsdgfdh"
            }
        ],
        "field_education": [
            {
                "value": "ytutyuii"
            }
        ],
        "field_emails": [
            {
                "value": "gtf@fgfg.com"
            }
        ],
        "field_experiece": [
            {
                "value": "fghtutyu"
            }
        ],
        "field_name": [
            {
                "value": "ytt"
            }
        ]
    }
]

格式为

[
    {
        "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\r\nuu;\r\nsdgfdh",
        "field_education":"ytutyuii",
        "field_emails":"gtf@fgfg.com",
        "field_experiece":"fghtutyu",
        "field_name":"ytt"
    }
]

使用php。然后我只能管理一个表格角js。提前致谢

2 个答案:

答案 0 :(得分:1)

试试这个

 $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\r\nuu;\r\nsdgfdh"
                }
            ],
            "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 :(得分:-2)

很简单。

<?php
$arr = array('nid' => 17, 'uuid' => '3614e0c8-88d4-4e8d-a732-5089698556d5', ...);

echo json_encode($arr);
?>

如果你有一些误解,请问我。