2016-11-07 61 views
0

JSON对象我不得不发送JSON在以下格式:编码数据转换成在PHP

{  
"suomottoRegVO" : { 
    "rgdtls" : { 
     "aplty" : APLSM 
     }, 
    "docls" : [ 
    { 
     "id" : 123, 
     "ty" : 101, 
    }, 
    { 
     "id" : 123, 
     "ty" : 101, 
    } 
    ], 
"todtls":{ 
    "tonm":"Gaurav Kumar", 
    "todg":"Tax Official", 
    "pl":"Bangalore", 
    "dt":" 12-04-2015 23:05:23" 
    } 
    } 
} 

我使用以下代码以它编码成JSON和自docls是数组,我使用的是循环将两个值放入其中。

$json_string = array(
        'suomottoRegVO' => array (
         'rgdtls'=> array(
          'aplty'=>'APLSM', 
         ), 
        'todtls' => array(
         'tonm' => 'Gaurav Kumar', 
         'todg' => 'Tax Official', 
         'pl' => 'Bangalore', 
         'dt' => ' 12-04-2015 23:05:23', 
         ) 
         ) 
        ); 

     for ($i=0; $i<2; $i++) { 
        $docls[] = array (
         'id' => '123', 
         'ty' => '101' 
         ); 
        } 
     $json_string['docls']=$docls; 
     json_encode($json_string); 

当我打印JSON,就变成:

{  
"suomottoRegVO" : { 
    "rgdtls" : { 
     "aplty" : APLSM 
    }, 
    "todtls":{ 
    "tonm":"Gaurav Kumar", 
    "todg":"Tax Official", 
    "pl":"Bangalore", 
    "dt":" 12-04-2015 23:05:23" 
    } 
} 
"docls" : [ 
    { 
    "id" : 123, 
    "ty" : 101, 
    }, 
    { 
    "id" : 123, 
    "ty" : 101, 
    } 
    ], 
} 

的 “docls” 应该是里面的 “suomottoRegVO”,但并非如此。请任何人指导我解决这个问题。

+0

为什么两次? '$ I <2'? – devpro

+0

用户可以上传'n'个文档。我只显示了2. –

回答

0

尝试...... 您需要的PaaS $json_string['suomottoRegVO']下你的阵列,试试这个方法...

$json_string['suomottoRegVO']['docls']=$docls; 
json_encode($json_string); 
+0

这工作! :) –