Php 接收错误“;不是JSONArray“;为post创建JSON数组时
我正在将数据格式化以发布到web api。在我看来它是正确的,但我得到了一个错误Php 接收错误“;不是JSONArray“;为post创建JSON数组时,php,json,rest,Php,Json,Rest,我正在将数据格式化以发布到web api。在我看来它是正确的,但我得到了一个错误 Error:“result”:“failed”,“Error_message”:org.json.JSONException:JSONObject[“leads”]不是JSONArray。} 以下是有关如何格式化我的数据的示例: {“leadtype”:“259”,“leads”:[{“first_name”:“John”,“last_name”:“Anderson”, “电子邮件”:“约翰。anderson@g-
Error:“result”:“failed”,“Error_message”:org.json.JSONException:JSONObject[“leads”]不是JSONArray。}
以下是有关如何格式化我的数据的示例:
{“leadtype”:“259”,“leads”:[{“first_name”:“John”,“last_name”:“Anderson”,
“电子邮件”:“约翰。anderson@g-pwatersa.com“,”公司“:”GP Waters“,”城市“:”新
“纽约州”:“纽约”,“地址1”:“201第一街N.”,“国家”:“联合国”
状态“}]}
下面是我使用echo查看结果的样子:
{“leadtype”:“Test”,“Lead”:{:first_name”:“first”,“last_name”:“last”,“company”:“company”,“email”:myemail@gmail.com“,”电话“:”8886664455“,”城市“:”休斯顿“,”州“:”德克萨斯“,”邮政编码“:”77222“,”国家“:”美国“,”来源“:”演示“}
代码如下:
$data = array('leadtype'=>$type);
$data['leads'] = array(':first_name'=>$firstname,
':last_name'=>$lastname,
':company'=>$company,
':email'=>$email,
':phone'=>$phone,
':city'=>$city,
':state'=>$state,
':zip'=>$zip,
':country'=>$country,
':source'=>$source);
$data_string = json_encode($data);
echo "<br>".$data_string."<br>";
$ch = curl_init('https://myapi.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
echo "<br>".$result."<br>"
$data=array('leadtype'=>$type);
$data['leads']=数组(':first_name'=>$firstname,
“:last_name”=>lastname$,
“:company”=>$company,
':email'=>$email,
“:phone”=>$phone,
“:city”=>$city,
“:state”=>$state,
“:zip”=>$zip,
“:country'=>$country,
':source'=>$source);
$data\u string=json\u encode($data);
回显“
”$data_字符串。“
”;
$ch=curl\u init('https://myapi.com');
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,“POST”);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,数组(
'内容类型:application/json',
“内容长度:”.strlen($data_string))
);
$result=curl\u exec($ch);
回显“
”$result。“
”
我不明白为什么当我使用
json\u encode()
时,它会说它不是一个JSONArray,你的leads
成员不是一个数组,而是一个散列。该示例显示了一个lead对象数组,您将传入一个对象
考虑以下情况:
$lead = array(':first_name'=>$firstname,
':last_name'=>$lastname,
':company'=>$company,
':email'=>$email,
':phone'=>$phone,
':city'=>$city,
':state'=>$state,
':zip'=>$zip,
':country'=>$country,
':source'=>$source);
$data['leads'] = array($lead);