在一个json字符串中从Web服务返回数据

时间:2017-07-10 09:49:11

标签: php

我想在一个json容器中使用php从Web服务返回数据。我的下面的脚本给了我像这样分开的每个数据

{"No":"01","Name":"JOSEPH"}{"No":"02","Name":"AMINU"}

但我希望它像这样回归

[{"No":"01","Name":"JOSEPH","No":"02","Name":"AMINU"}]

下面是我的脚本

try{
    $service = new NTLMSoapClient($pageURL);
     $params = array('filter' => array( 
                        array('Field' => 'District_Name',  'Criteria' => '')
                                       ), 
                     'setSize' => 2); //setSize =0 will return all rows - Can cause performance issue with large results set!
     $result = $service->ReadMultiple($params); 
     $resultSet = $result->ReadMultiple_Result->customer;

    if (is_array($resultSet)) { 
        foreach($resultSet as $item) { 
            $data=array('No' => $item->No,'Name' => $item->Name);
            echo json_encode($data);
            } 
        } 
        else { 

            echo json_encode('record not found');
        }
    }
catch (Exception $e) {
    echo "<hr><b>ERROR: SoapException:</b> [".$e."]<hr>";
    echo "<pre>".htmlentities(print_r($service->__getLastRequest(),1))."</pre>";
}

2 个答案:

答案 0 :(得分:0)

请尝试一下这个。

try{
    $service = new NTLMSoapClient($pageURL);
     $params = array('filter' => array( 
                        array('Field' => 'District_Name',  'Criteria' => '')
                                       ), 
                     'setSize' => 2); //setSize =0 will return all rows - Can cause performance issue with large results set!
     $result = $service->ReadMultiple($params); 
     $resultSet = $result->ReadMultiple_Result->customer;

    if (is_array($resultSet)) { 
       $jsonArray = [];
       foreach($resultSet as $item) { 
       $data = array('No' => $item->No,'Name' => $item->Name);
       $jsonArray = array_merge($jsonArray, $data);
    } 
    echo json_encode($jsonArray);

        } 
        else { 

            echo json_encode('record not found');
        }
    }
catch (Exception $e) {
    echo "<hr><b>ERROR: SoapException:</b> [".$e."]<hr>";
    echo "<pre>".htmlentities(print_r($service->__getLastRequest(),1))."</pre>";
}

答案 1 :(得分:-1)

try{
    $service = new NTLMSoapClient($pageURL);
     $params = array('filter' => array( 
                        array('Field' => 'District_Name',  'Criteria' => '')
                                       ), 
                     'setSize' => 2); //setSize =0 will return all rows - Can cause performance issue with large results set!
     $result = $service->ReadMultiple($params); 
     $resultSet = $result->ReadMultiple_Result->customer;

    if (is_array($resultSet)) {
        $data = array();
        foreach($resultSet as $item) { 
            $data = $data + array('No' => $item->No,'Name' => $item->Name);
        } 
        echo json_encode($data);
    } 
    else {
        echo json_encode('record not found');
    }
}
catch (Exception $e) {
    echo "<hr><b>ERROR: SoapException:</b> [".$e."]<hr>";
    echo "<pre>".htmlentities(print_r($service->__getLastRequest(),1))."</pre>";
}