[英]Sending data using ajax json and phrasing in php
I'm passing stringify JSON like this to my php file, 我正在将这样的stringify JSON传递给我的php文件,
[{"Country Code":"bob","Country":"503","Description":"bobby","\"Minute Rate":"oregon","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":"steve","Country":"707","Description":"stevie","\"Minute Rate":"california","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":"dsfd","Country":"342","Description":"dfdfs","\"Minute Rate":"dfdsfs","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":"sada","Country":"342","Description":"sdsad","\"Minute Rate":"dfsffd","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":""}]
How can i run a loop to fetch these results in php? 我如何运行循环以在php中获取这些结果?
Thank you! 谢谢!
json_decode function takes a json string for its first parameter and an optional boolean (true/false) for its second parameter. json_decode函数的第一个参数为json字符串,第二个参数为可选的布尔值(true / false)。 The second parameter, if set to true, returns the json string as an associative array, if it's not set it will return an object.
第二个参数(如果设置为true)将返回json字符串作为关联数组,如果未设置,它将返回一个对象。
json_array = json_decode($json, true); //Converts to array
foreach($json_array as $json){
echo $json['key']; //Key
echo $json->key; //Value of key
}
FYI 费耶
Try 尝试
json_decode(string, true);
Result will be an array and you can loop it. 结果将是一个数组,您可以循环它。
Try this.. 尝试这个..
$json='[{"Country Code":"bob","Country":"503","Description":"bobby","\"Minute Rate":"oregon","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":"steve","Country":"707","Description":"stevie","\"Minute Rate":"california","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":"dsfd","Country":"342","Description":"dfdfs","\"Minute Rate":"dfdsfs","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":"sada","Country":"342","Description":"sdsad","\"Minute Rate":"dfsffd","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":""}]';
$result=json_decode($json, true);
foreach($result as $value)
{
echo $value['Country Code'];
}
I think you should take a look at json_decode() 我认为您应该看看json_decode()
<?php
$jsonObject = json_decode($yourstring);
echo $jsonObject->{'json-key'};
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.