使用ajax json和php中的短语发送数据

时间:2015-01-20 07:08:12

标签: php jquery arrays ajax json

我将这样的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":""}]

如何运行循环以在php中获取这些结果?

谢谢!

4 个答案:

答案 0 :(得分:2)

json_decode 函数的第一个参数采用json字符串,第二个参数采用可选的boolean(true / false)。第二个参数,如果设置为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

json_decode

答案 1 :(得分:1)

尝试

json_decode(string, true);

结果将是一个数组,你可以循环它。

答案 2 :(得分:1)

试试这个..

$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'];
}

答案 3 :(得分:0)

我认为你应该看看json_decode()

<?php
$jsonObject = json_decode($yourstring);
echo $jsonObject->{'json-key'};
?>