当前位置: 首页 > 面试题库 >

json在php中解码

毋胜涝
2023-03-14
问题内容

我有以下json字符串,我只想从中检索电子邮件地址。我如何在php中做到这一点?

{"communications":{"communication":[{"@array":"true","@id":"23101384","@uri":"xyz/v1/Communications/1111","household":{"@id":"111111","@uri":"xyz/v1/Households/5465465"},"person":{"@id":"","@uri":""},"communicationType":{"@id":"1","@uri":"xyz/v1/Communications/CommunicationTypes/1","name":"Home Phone"},"communicationGeneralType":"Telephone","communicationValue":"1111","searchCommunicationValue":"2693240758","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/111111111","household":{"@id":"14436295","@uri":"xyz/v1/Households/11111"},"person":{"@id":"2222222","@uri":"xyz/v1/People/22222222"},"communicationType":{"@id":"2","@uri":"xyz/v1/Communications/CommunicationTypes/2","name":"Work Phone"},"communicationGeneralType":"Telephone","communicationValue":"11111","searchCommunicationValue":"789787987","listed":"false","communicationComment":null,"createdDate":"2009-08-09T15:49:27","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/11111","household":{"@id":"1111","@uri":"xyz/v1/Households/1111"},"person":{"@id":"244404","@uri":"xyz/v1/People/1111"},"communicationType":{"@id":"3","@uri":"xyz/v1/Communications/CommunicationTypes/3","name":"Mobile"},"communicationGeneralType":"Telephone","communicationValue":"22222","searchCommunicationValue":"5475454","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"15454","@uri":"xyz/v1/Communications/111111","household":{"@id":"14436295","@uri":"xyz/v1/Households/1111"},"person":{"@id":"244444474","@uri":"xyz/v1/People/111111"},"communicationType":{"@id":"4","@uri":"xyz/v1/Communications/CommunicationTypes/4","name":"Email"},"communicationGeneralType":"Email","communicationValue":"email@needthis.com","searchCommunicationValue":"email@needthis.com","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:39:06"}]}}

问题答案:

另一种错误的做法是像这样访问它:

$json_object = '{"communications":
                       {"communication":
                         [{"@array":"true","@id":"23101384","@uri":"xyz/v1/Communications/1111","household":
                            {"@id":"111111","@uri":"xyz/v1/Households/5465465"},
                            "person": {"@id":"","@uri":""},
                            "communicationType":{"@id":"1","@uri":"xyz/v1/Communications/CommunicationTypes/1","name":"Home Phone"},
                            "communicationGeneralType":"Telephone","communicationValue":"1111","searchCommunicationValue":"2693240758",
                               "listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},
                            {"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/111111111","household":
                              {"@id":"14436295","@uri":"xyz/v1/Households/11111"},
                            "person": {"@id":"2222222","@uri":"xyz/v1/People/22222222"},
                            "communicationType": {"@id":"2","@uri":"xyz/v1/Communications/CommunicationTypes/2","name":"Work Phone"},
                            "communicationGeneralType":"Telephone","communicationValue":"11111","searchCommunicationValue":"789787987","listed":"false",
                                "communicationComment":null,"createdDate":"2009-08-09T15:49:27","lastUpdatedDate":"2009-08-11T23:40:02"},
                            {"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/11111","household": {"@id":"1111","@uri":"xyz/v1/Households/1111"},
                            "person":{"@id":"244404","@uri":"xyz/v1/People/1111"},
                            "communicationType":{"@id":"3","@uri":"xyz/v1/Communications/CommunicationTypes/3","name":"Mobile"},
                            "communicationGeneralType":"Telephone","communicationValue":"22222","searchCommunicationValue":"5475454","listed":"true",
                                "communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},
                            {"@array":"true","@id":"15454","@uri":"xyz/v1/Communications/111111","household":{"@id":"14436295","@uri":"xyz/v1/Households/1111"},
                            "person":{"@id":"244444474","@uri":"xyz/v1/People/111111"},
                            "communicationType":{"@id":"4","@uri":"xyz/v1/Communications/CommunicationTypes/4","name":"Email"},
                            "communicationGeneralType":"Email","communicationValue":"email@needthis.com","searchCommunicationValue":"email@needthis.com","listed":"true",
                            "communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:39:06"}
                         ]
                       }
                      }';


    $json_decoded = json_decode($json_object);
    echo "email: ".$json_decoded->communications->communication[3]->communicationValue."<br />";


 类似资料:
  • 问题内容: 我有以下JSON字符串: 如何在PHP中解析它并提取s 列表? 问题答案: 您可以使用该函数来解析PHP中的JSON数据(至少=> 5.2.0)。一旦有了PHP对象,就可以轻松地遍历所有配方/成员并使用以下内容访问其标题: (对不起,我现在无法真正运行此代码。希望无论如何它都会有所帮助。)

  • 问题内容: json_decode函数不是PHP 5.1的一部分,所以我不能使用它。此版本还有其他功能吗? 问题答案: 在PHP 5.2之前,您可以使用JSON PECL扩展名。 实际上,它是已集成到PHP 5.2 _( 引用 )中_的扩展: 从PHP 5.2.0开始,JSON扩展默认捆绑并编译为PHP。 其他一些解决方案是使用PHP开发的某些组件。 前一段时间(大约一年前),我使用Zend Fr

  • 问题内容: 我有以下脚本从API获取搜索结果,然后切片该数组并将其转储,我在将JSON解码为数组时遇到问题,它返回 这是一个wordpress简码 这是从api获得的Json示例: 以下是用于获取JSON和对其进行分页的代码: 问题答案: 试试json_decode 然后,您将获得一个数组而不是一个对象。 Example#1 json_decode()示例 上面的示例将输出:

  • 问题内容: 我有一个看起来像这样的JSON字符串: 什么是解码此和地点PHP ,,,,和到会话变量? 到目前为止,我已经尝试过了,但是没有用: 谢谢! 问题答案: 是您了解JSON结构的朋友。 结果是:

  • 问题内容: 我返回数组数据类型从到,我曾经将其转换为一个关联数组,但是当我尝试使用关联使用它,我得到的错误返回的数据看起来像这样 请,我如何访问这样的数组?感谢您的任何建议。 问题答案: 在上面的示例中,将您作为第二个参数传递给您时,您可以执行类似以下操作来检索数据: 如果您不将其作为第二个参数传递,则将其作为对象返回:

  • 问题内容: 我试图从提供格式数据的Web服务请求天气。我的PHP请求代码失败了: 这是返回的一些数据。为了简洁起见,一些细节已被截断,但保留了对象完整性: 问题答案: 这似乎起作用: 如果将json_decode的第二个参数设置为true,则会得到一个数组,因此无法使用->语法。我还建议您安装JSONview Firefox扩展 ,以便您可以以类似于 Firefox显示XML结构的漂亮格式的树状视