将从android接收的字符串转换为jsonArray并在php中循环它

时间:2015-05-18 10:11:43

标签: php parsing

我使用此命令在php中以字符串形式接收以下内容

$jsonString = file_get_contents('php://input');

我收到的字符串并不完全是json格式。它包含introductionesJson = [],这是唯一的。

我收到的字符串如下:

introduceesJson=
[
{"oneWay":"1","destNumber":"98 65 326569","infoName":"Aa",
"infoNumber":"96 35"},
{"oneWay":"1","destNumber":"98 65 326569","infoName":"Bb",
"infoNumber":"82 77"},
{"appVersion":"1.0","packetType":"1","packetVersion":"1.0",
"deviceType":"android"}
]

现在,我想将所有细节存储在一个数组中。任何帮助,将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:1)

使用json_decode

将json字符串读取/映射到php对象

示例

<?php

$jsonString = file_get_contents('php://input');

$json = str_replace("introduceesJson=", "", $jsonstring);

$ob1 = json_decode($json, true);

// add your application-specific code for processing $ob1 here..

var_dump($ob1);

?>

然后,您可以添加特定于应用程序的逻辑来处理php对象

答案 1 :(得分:1)

$introduceesJson=
'[
{"oneWay":"1","destNumber":"98 65 326569","infoName":"Aa",
"infoNumber":"96 35"},
{"oneWay":"1","destNumber":"98 65 326569","infoName":"Bb",
"infoNumber":"82 77"},
{"appVersion":"1.0","packetType":"1","packetVersion":"1.0",
"deviceType":"android"}
            ]';
        $resultArray = json_decode($introduceesJson, true);
    foreach($resultArray as $key => $value)
    {
    foreach($value as $k=> $v)
    //your key and values are here 
    }