【问题标题】:Convert Array to JSON string php将数组转换为 JSON 字符串 php
【发布时间】:2016-04-28 19:39:27
【问题描述】:

我有一个这样的数组

$prebook=array(
             'sourceCity'=>$_POST['source'], 
             'destinationCity'=>$_POST['dest'],
             'doj'=>$_POST['doj'],
             'routeScheduleId'=>$_POST['routeid'],
              'boardingPoint'=>array(
                  'id'=>$id,
                  'location'=>$location,
                  'time'=>$time
              ),     
              'customerName'=>$_POST['fname'],
              'customerLastName'=>$_POST['lname'],
              'customerEmail'=>$_POST['email'],
              'customerPhone'=>$_POST['mobileno'],
              'emergencyPhNumber'=>$_POST['emc-number'],
              'customerAddress'=>$_POST['address'],
              'blockSeatPaxDetails'=>array(array(
                  'age'=>$_POST['age'][$key],
                  'name'=>$value,
                  'seatNbr'=>$_POST['seat-no'][$key],
                  'Sex'=>$_POST['gender'.$no],
                  'fare'=>$_POST['base-fare'][$key],
                  'totalFareWithTaxes'=>$_POST['amount'][$key],
                  'ladiesSeat'=>$ladies,
                  'lastName'=>$_POST['plname'][$key],
                  'mobile'=>$_POST['mobileno'],
                  'title'=>'Mr',
                  'email'=>$_POST['email'],
                  'idType'=>$_POST['idtype'],
                  'idNumber'=>$_POST['id-number'],
                  'nameOnId'=>$value,
                  'primary'=>true,
                  'ac'=>$ac,
                  'sleeper'=>$sleeper
         )),
                'inventoryType'=>$_POST['invtype']             
           )

从这里我想得到 Json 字符串看起来像这样

apiBlockTicketRequest:{"sourceCity":"Hyderabad","destinationCity":"Bangalore","doj":"2016-01-22","routeScheduleId":"6717","boardingPoint":{"id":"2889","location":"Mettuguda,Opp. Mettuguda Church","time":"04:50PM"},"customerName":"jj","customerLastName":"jjj","customerEmail":"shamonsha665@gmail.com","customerPhone":"7779","emergencyPhNumber":"7878","customerAddress":"gjgj","blockSeatPaxDetails":[{"age":"22","name":"hjhj","seatNbr":"G4","Sex":"F","fare":"900","totalFareWithTaxes":"945","ladiesSeat":false,"lastName":"hjhj","mobile":"7779","title":"Mr","email":"shamonsha665@gmail.com","idType":"Aadhar Card","idNumber":"jkjk","nameOnId":"hjhj","primary":true,"ac":false,"sleeper":false}],"inventoryType":"0"}

这是我的代码

$data =json_encode($prebook);
$json='apiBlockTicketRequest:'.$data;
echo $json;

但是当我使用this 验证 JSON 字符串时,我会收到以下错误

期望对象或数组,而不是字符串。[代码 1,结构 1]

错误:字符串应该用双引号括起来。

【问题讨论】:

  • $json='apiBlockTicketRequest'.$data; 这就是为什么
  • 查看预期的Json字符串
  • 也就是说你开始手工制作了?
  • $json='{apiBlockTicketRequest:'.$data.'}'; 应该更好。尽管编写自己的 JSON 通常是个坏主意。让json_encode 完成所有工作。
  • 但得到同样的错误

标签: php arrays json


【解决方案1】:

您通过将apiBlockTicketRequest 添加到输出来创建无效的json

 $json='apiBlockTicketRequest'.$data;

你可以这样做

$json = json_encode(['apiBlockTicketRequest' => $prebook]);

【讨论】:

  • 如果你有 php array() 而不是 []
  • 但是我不想{在字符串的开头和结尾,bcz这个json是api服务的请求,当我传递这个json时,我会从api得到一个错误,
  • 抱歉,请不要使用该服务,因为它要求您生成 invalid JSON
  • 在乞讨时需要使用 { ,所以您询问的是 pseudo json 而不是 JSON,因为 @Hanky웃Panky 提到了
【解决方案2】:

你的代码是正确的string,但整个输出字符串($json)永远不会解析为json,只有你编码的$data

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多