将 JSON 字符串转换为 PHP 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9170639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert JSON string to PHP Array
提问by Snowman
I have the following JSON string, which was an Objective C array then encoded to JSON:
我有以下 JSON 字符串,它是一个 Objective C 数组,然后编码为 JSON:
$jsonString=[\"[email protected]\",\"[email protected]\",\"[email protected]\"]
I want to convert this to a regular PHP array. I've tried many things but none of them seem to work:
我想将其转换为常规 PHP 数组。我尝试了很多东西,但似乎没有一个起作用:
$arrayOfEmails=json_decode($jsonString); //doesn't work
$arrayOfEmails=(array)json_decode($jsonString); //doesn't work
Any ideas?
有任何想法吗?
Edit:
编辑:
I'm still not getting it to work.
我仍然没有让它工作。
$decodeEmails=$_POST["arrayOfEmails"];
sendResponse(200, $decodeEmails);
//the above returns exactly whats after this colon:[\"[email protected]\",\"[email protected]\",\"[email protected]\]
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?
我需要这样做: $arrayOfEmails=json_decode($decodeEmails);
但我认为我需要在 $decodedEmails 周围引用才能使其工作。如何在 $decodeEmails 字符串周围添加引号?
回答by the_wizard
Try this:
json_decode($json_string, true);
尝试这个:
json_decode($json_string, true);
回答by xdazz
You should quote your string, it works fine, see here.
您应该引用您的字符串,它工作正常,请参见此处。
$jsonString = '["[email protected]","[email protected]","[email protected]"]';
$arrayOfEmails=json_decode($jsonString);
Or
或者
$jsonString = "[\"[email protected]\",\"[email protected]\",\"[email protected]\"]";
$arrayOfEmails=json_decode($jsonString);
回答by kranthi kumar pulivarhty
$data = unserialize($data)
now u can get the $data as array
现在你可以得到 $data 作为数组
For example $data have the value like this
例如 $data 有这样的值
$data = "a:2:{s:18:"_1337666504149_149";a:2:{s:8:"fbregexp";s:1:"1";s:5:"value";s:4:"2222";}s:18:"_1337666505594_594";a:2:{s:8:"fbregexp";s:1:"3";s:5:"value";s:5:"45555";}}";
$data = "a:2:{s:18:"_1337666504149_149";a:2:{s:8:"fbregexp";s:1:"1";s:5:"value";s:4: "2222";}s:18:"_1337666505594_594";a:2:{s:8:"fbregexp";s:1:"3";s:5:"value";s:5:"45555"; }}";
$data = unserialize($data)
now i get value like this
现在我得到了这样的价值
Array ( [fbregexp] => 1 [value] => 2222 ) [_1337666505594_594] => Array ( [fbregexp] => 3 [value] => 45555 ) )
回答by bortunac
$str=<<<H
{"a":"AAA","b":"333"}
H;
$object = json_decode($str);
$array = json_decode($str , 1 );
// $arr = get_object_vars( json_decode($str) );
回答by mintedsky
You could use json_decode() then print_r() to create a PHP formatted array
您可以使用 json_decode() 然后使用 print_r() 创建一个 PHP 格式的数组
<?php
$json = file_get_contents('json/yourscript.json'); // Get the JSON data
$phpObj = json_decode($json,true); // Convert to PHP Object
print_r($phpObj); // Convert to PHP Array
?>
回答by Will
If json_decode
isn't working, you could try something like this:
如果json_decode
不起作用,您可以尝试以下操作:
$arr = explode( '\",\"', substr( $json, 3, strlen( $json ) - 3 ) );
回答by JAAulde
Assuming the lack of quotes around your JSON in the question is a transposition error during posting, then the code you're using is fine: http://codepad.org/RWEYM42x
假设问题中 JSON 周围缺少引号是发布期间的转置错误,那么您使用的代码很好:http: //codepad.org/RWEYM42x
You do need to ensure your string is UTF8 encoded. You can use the built in encoder if it isn't ( http://php.net/manual/en/function.utf8-encode.php).
您确实需要确保您的字符串是 UTF8 编码的。如果不是,您可以使用内置编码器(http://php.net/manual/en/function.utf8-encode.php)。
For any further help, you need to actually tell us what you aregetting with your code.
对于任何进一步的帮助,实际上你需要告诉我们你什么都与您的代码获得。
回答by Prasad Rajapaksha
This code works fine.
这段代码工作正常。
$jsonString = '["[email protected]","[email protected]","[email protected]"]';
$arrayOfEmails=json_decode($jsonString);
$arrayOfEmails=(array)json_decode($jsonString);
print_r($arrayOfEmails);
回答by shojol80
$arr = explode( '\",\"', substr( $json, 3, strlen( $json ) - 3 ) )
This solution is good but for getting full valid array I'm using strlen( $json ) - 6
, so it should be:
这个解决方案很好,但为了获得我正在使用的完整有效数组strlen( $json ) - 6
,所以它应该是:
$arr = explode( '\",\"', substr( $json, 3, strlen( $json ) - 6 ) );
var_dump($arr);