将动态JSON对象解析为PHP

时间:2014-03-17 18:58:57

标签: php json

下面是我试图解析的JSON对象。它基本上是动态形式的答案。我知道我可以将它转换为数组,然后使用它。我遇到的问题是JSON对象每次都会有所不同。

{
"sections": [
    {
        "elements": [
            {
                "element": "greeting",
                "components": {
                    "paragraph": {
                        "content": "Thank you for calling company name. This is a test call."
                    }
                }
            },
            {
                "element": "custom-fullname",
                "components": {
                    "label": {
                        "content": ""
                    },
                    "first_name": {
                        "name": "1[custom-fullname][first_name]",
                        "label": "First Name",
                        "type": "text",
                        "value": "John",
                        "required": ""
                    },
                    "last_name": {
                        "name": "1[custom-fullname][last_name]",
                        "label": "Last Name",
                        "type": "text",
                        "value": "smith",
                        "required": ""
                    },
                    "help_text": {
                        "content": ""
                    }
                }
            },
            {
                "element": "custom-phone",
                "components": {
                    "label": {
                        "content": "Phone Number"
                    },
                    "phone_ext": {
                        "name": "2[custom-phone][phone_ext]",
                        "type": "text",
                        "value": ""
                    },
                    "input_format": {
                        "name": "2[custom-phone][phone_format]",
                        "type": "hidden",
                        "value": "standard"
                    },
                    "help_text": {
                        "content": ""
                    },
                    "phone": {
                        "value": "7864564444",
                        "required": "false",
                        "name": "2[custom-phone][phone]"
                    }
                }
            },
            {
                "element": "custom-message",
                "components": {
                    "label": {
                        "content": "Message"
                    },
                    "message": {
                        "name": "3[custom-message][message]",
                        "type": "text",
                        "value": "test time 22222",
                        "required": ""
                    },
                    "help_text": {
                        "content": ""
                    }
                }
            },
            {
                "element": "notes",
                "components": {
                    "paragraph": {
                        "content": "This is a test call. Please mark this as a test."
                    }
                }
            }
        ],
        "faqs": [
            {
                "question": "Should i have a FAQ?",
                "answer": "Yes, this will help the agent to have a fluent conversation with the caller."
            }
        ]
    },
    {
        "elements": [
            {
                "element": "greeting",
                "components": {
                    "paragraph": {
                        "content": "Thank you for calling company name. This is a test call."
                    }
                }
            },
            {
                "element": "custom-fullname",
                "components": {
                    "label": {
                        "content": ""
                    },
                    "first_name": {
                        "name": "1[custom-fullname][first_name]",
                        "label": "First Name",
                        "type": "text",
                        "value": "robert",
                        "required": ""
                    },
                    "last_name": {
                        "name": "1[custom-fullname][last_name]",
                        "label": "Last Name",
                        "type": "text",
                        "value": "gonzalez",
                        "required": ""
                    },
                    "help_text": {
                        "content": ""
                    }
                }
            },
            {
                "element": "custom-phone",
                "components": {
                    "label": {
                        "content": "Phone Number"
                    },
                    "phone_ext": {
                        "name": "2[custom-phone][phone_ext]",
                        "type": "text",
                        "value": ""
                    },
                    "input_format": {
                        "name": "2[custom-phone][phone_format]",
                        "type": "hidden",
                        "value": "standard"
                    },
                    "help_text": {
                        "content": ""
                    },
                    "phone": {
                        "value": "7864564444",
                        "required": "false",
                        "name": "2[custom-phone][phone]"
                    }
                }
            },
            {
                "element": "custom-message",
                "components": {
                    "label": {
                        "content": "Message"
                    },
                    "message": {
                        "name": "3[custom-message][message]",
                        "type": "text",
                        "value": "Test",
                        "required": ""
                    },
                    "help_text": {
                        "content": ""
                    }
                }
            },
            {
                "element": "notes",
                "components": {
                    "paragraph": {
                        "content": "This is a test call. Please mark this as a test."
                    }
                }
            }
        ],
        "faqs": [
            {
                "question": "Should i have a FAQ?",
                "answer": "Yes, this will help the agent to have a fluent conversation with the caller."
            }
        ]
    },
    {
        "elements": [
            {
                "element": "greeting",
                "components": {
                    "paragraph": {
                        "content": "Thank you for calling company name. This is a test call."
                    }
                }
            },
            {
                "element": "custom-fullname",
                "components": {
                    "label": {
                        "content": ""
                    },
                    "first_name": {
                        "name": "1[custom-fullname][first_name]",
                        "label": "First Name",
                        "type": "text",
                        "value": "robert",
                        "required": ""
                    },
                    "last_name": {
                        "name": "1[custom-fullname][last_name]",
                        "label": "Last Name",
                        "type": "text",
                        "value": "gonzalez",
                        "required": ""
                    },
                    "help_text": {
                        "content": ""
                    }
                }
            },
            {
                "element": "custom-phone",
                "components": {
                    "label": {
                        "content": "Phone Number"
                    },
                    "phone_ext": {
                        "name": "2[custom-phone][phone_ext]",
                        "type": "text",
                        "value": ""
                    },
                    "input_format": {
                        "name": "2[custom-phone][phone_format]",
                        "type": "hidden",
                        "value": "standard"
                    },
                    "help_text": {
                        "content": ""
                    },
                    "phone": {
                        "value": "7861238975",
                        "required": "false",
                        "name": "2[custom-phone][phone]"
                    }
                }
            },
            {
                "element": "custom-message",
                "components": {
                    "label": {
                        "content": "Message"
                    },
                    "message": {
                        "name": "3[custom-message][message]",
                        "type": "text",
                        "value": "test time 22222",
                        "required": ""
                    },
                    "help_text": {
                        "content": ""
                    }
                }
            },
            {
                "element": "radio",
                "components": {
                    "label": {
                        "content": "Whats type of office are you looking for?"
                    },
                    "radiolist": [
                        {
                            "input": {
                                "value": "value_0",
                                "id": "single_choice_answer_0"
                            },
                            "label": {
                                "content": "Dedicated Space"
                            }
                        },
                        {
                            "input": {
                                "value": "value_1",
                                "id": "single_choice_answer_1"
                            },
                            "label": {
                                "content": "Parttime Space"
                            }
                        }
                    ],
                    "single_choice_answer": {
                        "value": "value_0",
                        "name": "4[radio][single_choice_answer]",
                        "required": ""
                    },
                    "help_text": {
                        "content": ""
                    }
                }
            }
        ],
        "faqs": [
            {
                "question": "Should i have a FAQ?",
                "answer": "Yes, this will help the agent to have a fluent conversation with the caller."
            }
        ]
    }
]


}

因此,例如,JSON对象的一部分如下所示。

                    "components": {
                    "label": {
                        "content": "Whats type of office are you looking for?"
                    },
                    "radiolist": [
                        {
                            "input": {
                                "value": "value_0",
                                "id": "single_choice_answer_0"
                            },
                            "label": {
                                "content": "Dedicated Space"
                            }
                        },
                        {
                            "input": {
                                "value": "value_1",
                                "id": "single_choice_answer_1"
                            },
                            "label": {
                                "content": "Parttime Space"
                            }
                        }
                    ],
                    "single_choice_answer": {
                        "value": "value_0",
                        "name": "4[radio][single_choice_answer]",
                        "required": ""
                    },

我想接受并执行以下操作:

    {
"components": {
    "label": {
        "content": "Whats type of office are you looking for?"
    },
    "answer": {
        "value": "value_0",
        "name": "Dedicated Space",
        "required": ""
    }
}

真的,我只需要在JSON对象中格式化一点形式问题和答案。但是,我不确定如何在PHP中编写代码。

0 个答案:

没有答案