将JSON字符串转换为PHP数组 [英] Convert JSON string to PHP Array
本文介绍了将JSON字符串转换为PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下JSON字符串,它是一个Objective C数组,然后编码为JSON:
I have the following JSON string, which was an Objective C array then encoded to JSON:
$jsonString=[\"a@gmail.com\",\"b@gmail.com\",\"c@gmail.com\"]
我想将其转换为常规的PHP数组.我已经尝试了很多东西,但似乎都没有用:
I want to convert this to a regular PHP array. I've tried many things but none of them seem to work:
$arrayOfEmails=json_decode($jsonString); //doesn't work
$arrayOfEmails=(array)json_decode($jsonString); //doesn't work
有什么想法吗?
我仍然无法正常工作.
$decodeEmails=$_POST["arrayOfEmails"];
sendResponse(200, $decodeEmails);
//the above returns exactly whats after this colon:[\"a@gmail.com\",\"b@gmail.com\",\"c@gmail.com\]
我需要这样做:$arrayOfEmails=json_decode($decodeEmails);
但是我想我需要在$ decodedEmails周围加引号才能使它起作用.如何在$ decodeEmails字符串周围添加引号?
I need to do this: $arrayOfEmails=json_decode($decodeEmails);
But I think I need quotes around $decodedEmails for this to work. How can I add quotes around $decodeEmails string?
推荐答案
You should quote your string, it works fine, see here.
$jsonString = '["m@gmail.com","b@gmail.com","c@gmail.com"]';
$arrayOfEmails=json_decode($jsonString);
或
$jsonString = "[\"a@gmail.com\",\"b@gmail.com\",\"c@gmail.com\"]";
$arrayOfEmails=json_decode($jsonString);
这篇关于将JSON字符串转换为PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文