我正在綁定格式化數據以發佈到web api。它看起來是正確的,但我得到一個錯誤。創建JSON數組後發送錯誤「not a JSONArray」
Error: "result":"failed","error_message":org.json.JSONException: JSONObject["leads"] is not a JSONArray.}
這裏是樣品到我的數據應該如何格式化:
{"leadtype":"259","leads":[{"first_name":"John", "last_name":"Anderson", "email":"[email protected]", "company":"GP Waters", "city":"New York", "state":"New York", "address1":"201 1st St. N.", "country":"United States"}]}
而且這裏是我的樣子使用echo看到的結果:
{"leadtype":"Test","leads":{":first_name":"First",":last_name":"Last",":company":"Company",":email":"[email protected]",":phone":"8886664455",":city":"Houston",":state":"TX",":zip":"77222",":country":"USA",":source":"Demo"}}
這裏是代碼:
$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>"
我不明白爲什麼它說它不是一個JSONArray當我使用json_encode()
。
謝謝這個工作。 – Steven 2013-04-26 15:35:21