我为我的运输报价系统构建了一个API。 我给它提供了价值,我得到了报价。 它工作正常但我无法解码JSON响应。 我得到一个NULL响应,我不确定是什么。 根据验证者的说法,JSON是正确的。
所以我基本上做的是在一侧编码PHP数组,我想在另一端使用浏览器和PHP解析它。 但是,我得到一个NULL响应。
这是JSON。 如果您需要更多,请告诉我。
{"carrier":"R&L Carriers","charge":"99.13","service_days":"Wednesday Oct. 22, 2014"}我只想解码这个,所以我可以解析它。 如果还有其他解析方法请告诉我。
此外,我搜索了SOF,人们在这里遇到的类似问题对我没有帮助。
这是我用来生成JSON的代码。
<?php //include ('mysql_connect.php'); $result = mysql_query('select * from quote where user_id = "'.$user_id.'" order by netCharge asc limit 1'); if (!$result) { die('Could not query:' . mysql_error()); } if (!$result) echo mysql_error(); $api_data = array(); $api_count = '0'; while ($row = mysql_fetch_array($result, MYSQLI_ASSOC)) { $api_data[carrier] = $row['carrier']; $api_data[charge] = $row['netCharge']; $api_data[service_days] = $row['serviceDays']; $api_count++; } $api_data = json_encode($api_data); print_r($api_data); ?>这就是我用来获取JSON数据的方法:
<?php $input = file_get_contents('api_request.php?dest_zip=66101&weight=200&class=50<l_shipment=X&Residential_Delivery=X'); echo $input; $obj = json_decode($input); var_dump($obj); ?>I have built an API for my shipping quote system. I feed it values and I get rate quotes back. It works fine but I cannot decode the JSON response. I get a NULL response and I'm not sure whats up. According to validaters the JSON is correct.
So what I essentially do is encode a PHP array on one side and I want to parse that on the other side using a browser and PHP. However, I get a NULL response.
Here is the JSON. Let me know if you need more.
{"carrier":"R&L Carriers","charge":"99.13","service_days":"Wednesday Oct. 22, 2014"}I just want to decode this so I can parse it. If there is another way to parse please let me know.
Also, I searched SOF and the similar issues people were having here didn't help me.
This is the code I use to generate the JSON.
<?php //include ('mysql_connect.php'); $result = mysql_query('select * from quote where user_id = "'.$user_id.'" order by netCharge asc limit 1'); if (!$result) { die('Could not query:' . mysql_error()); } if (!$result) echo mysql_error(); $api_data = array(); $api_count = '0'; while ($row = mysql_fetch_array($result, MYSQLI_ASSOC)) { $api_data[carrier] = $row['carrier']; $api_data[charge] = $row['netCharge']; $api_data[service_days] = $row['serviceDays']; $api_count++; } $api_data = json_encode($api_data); print_r($api_data); ?>This is what I'm using to grab that JSON data:
<?php $input = file_get_contents('api_request.php?dest_zip=66101&weight=200&class=50<l_shipment=X&Residential_Delivery=X'); echo $input; $obj = json_decode($input); var_dump($obj); ?>最满意答案
你试过以下吗?
$array = json_decode($data, true);When I wrote the code I used 'Enter' to give me spaces between lines of code. For some reason that translated to the JSON and that's why it wasn't working. I just deleted all the extra empty lines in my file and it worked.
更多推荐
发布评论