简体   繁体   English

如何解码json字符串?

[英]How to decode json string?

I am working on one project. 我正在做一个项目。 In which I am getting data in json string format from android or iOs application. 我从android或iOs应用程序中以json字符串格式获取数据。 I am using json decode to decode it. 我正在使用json解码对其进行解码。 But I didnt get any array. 但是我没有得到任何数组。

ioS and Android calls following URL and pass data in REQUEST mode through URL. ioS和Android会在URL之后调用并以URL的请求模式传递数据。 Url is as follows : 网址如下:

http://dummy.test.pro/webservices/Cart/AddToCartMatrix?{ "Add_Cart" = "{\n \"product_id\" : \"30\",\n \"user_id\" : \"67\",\n \"design_inventory\" : \"design_master\",\n \"table\" : \"temp_cart\"\n}"; }

In PHP, I am reading data as follows : 在PHP中,我正在读取数据,如下所示:

$cart_data = json_decode(stripslashes($_REQUEST['Add_Cart']));
pre($cart_data);

But After calling URL , I am getting following error : 但是在调用URL之后,出现以下错误:

Message: Undefined index: Add_Cart 消息:未定义的索引:Add_Cart

Please help me in this issue. 请帮我解决这个问题。 Thanks in advance. 提前致谢。

This means that Add_Cart isn't an index in your $_REQUEST array. 这意味着Add_Cart不是$ _REQUEST数组中的索引。

why don't you use something like print_r($_REQUEST) to check the content of your request? 为什么不使用诸如print_r($_REQUEST)来检查print_r($_REQUEST)的内容?

I'm uncertain of how you are sending the request but handling the request but I think changing how you send your request uri from. 我不确定您如何发送请求但如何处理请求,但是我认为应该更改发送请求uri的方式。

http://dummy.test.pro/webservices/Cart/AddToCartMatrix?{ "Add_Cart" = "{\n \"product_id\" : \"30\",\n \"user_id\" : \"67\",\n \"design_inventory\" : \"design_master\",\n \"table\" : \"temp_cart\"\n}"; }

to: 至:

http://dummy.test.pro/webservices/Cart/AddToCartMatrix?Add_Cart=%7B%5Cn+%5C%22product_id%5C%22+%3A+%5C%2230%5C%22%2C%5Cn+%5C%22user_id%5C%22+%3A+%5C%2267%5C%22%2C%5Cn+%5C%22design_inventory%5C%22+%3A+%5C%22design_master%5C%22%2C%5Cn+%5C%22table%5C%22+%3A+%5C%22temp_cart%5C%22%5Cn%7D

Read more about why this may work here 在此阅读更多有关为何可能起作用的信息

作品 在此处输入图片说明

In my own tests this method works. 在我自己的测试中,此方法有效。 Ensure that you call some URL encoding function for the JSON and make Add_Cart as the variable name of the json. 确保为JSON调用一些URL编码函数,并将Add_Cart作为json的变量名。 (not wrapping it in JSON as it) (不将其包装在JSON中)

This iOS/Android app is sending a pretty malformed URL which does not use any known or valid query string format. 这个iOS / Android应用程式传送的网址格式错误,没有使用任何已知或有效的查询字串格式。 That means that you cannot rely on PHP to decode it for you into $_GET or $_REQUEST . 这意味着您不能依靠PHP为您将其解码为$_GET$_REQUEST

Unless you can contact the app creators to get it fixed, you'll have to fetch the raw data from the URL (if GET): 除非您可以与应用程序创建者联系以进行修复,否则必须从URL(如果是GET)中获取原始数据:

$json = $_SERVER['QUERY_STRING']

... or the request body (if POST): ...或请求正文(如果为POST):

$json = file_get_contents("php://input");

However, it's still possible (I'd say likely) that data has got corrupted on transport and further json_decode() fails. 但是,仍然有可能(我想说)数据在传输中已损坏,并且进一步json_decode()失败。 If so, you'll need additional string replacements to try and get it fixed. 如果是这样,您将需要其他字符串替换来尝试修复它。

Try This 尝试这个

http://dummy.test.pro/webservices/Cart/AddToCartMatrix?Add_Cart={\n \"product_id\" : \"30\",\n \"user_id\" : \"67\",\n \"design_inventory\" : \"design_master\",\n \"table\" : \"temp_cart\"\n}

you might missed get method key 您可能会错过获取方法密钥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM