无法解码JSON API响应(Cannot Decode JSON API response)

编程入门 行业动态 更新时间:2024-10-22 17:31:49
无法解码JSON API响应(Cannot Decode JSON API response)

我为我的运输报价系统构建了一个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&ltl_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&ltl_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.

更多推荐

本文发布于:2023-08-04 05:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409379.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:API   JSON   response   Decode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!