在PHP中解析转义的JSON [英] Parse escaped JSON in PHP
本文介绍了在PHP中解析转义的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是PHP世界中的新手,无法解析PHP中的JSON.我想使用Apache HttpClient 4.x
和Gson
使用Java客户端将数据发布到PHP脚本.
I'm a newb in PHP world and I've problems parsing JSON in PHP. I want to POST data to PHP script with my Java client using Apache HttpClient 4.x
and Gson
.
我的JSON:
[{"Knt_KntWatchId":"15","type":"INSERT","Knt_Nazwa1":"a"},{...},...]
我正在使用HttpClient
和Gson
用Java发送它:
I'm sending it with Java using HttpClient
and Gson
:
List<Contact> contacts = ...;
HttpPost post = new HttpPost(CONTACTS_SERVICE);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("key", AppConstants.KEY));
nameValuePairs.add(new BasicNameValuePair("data", new Gson().toJson(contacts)));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
在页面上:
if (isset($_POST['data'])) {
$data = $_POST['data'];
$json = json_decode($data, true);
var_dump($json);
var_dump($data);
我得到的是:
NULL string(3651) "[{\"Knt_KntWatchId\":\"15\",\"type\":\"INSERT\",\"Knt_Nazwa1\":\"a\"},...
我如何使其正常工作?
PHP 5.2-无法使用json_last_error()
PHP 5.2 - can't use json_last_error()
推荐答案
尝试使用 stripslashes()
$string = stripslashes('[{\"Knt_KntWatchId\":\"15\",\"type\":\"INSERT\",\"Knt_Nazwa1\":\"a\"}]');
print_r(json_decode($string));
所以我会
$json = json_decode(stripslashes($data), true);
这篇关于在PHP中解析转义的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文