php - 将JSON数组转换为String和JSON再次android
问题描述
我正在向我创建的本地 php 服务器发送一些帖子数据。我希望帖子数据采用 json 格式,以便以后通过 php 轻松获取这些数据。使用Namevaluepairs
,我将我的 jsonArray 转换为字符串,并在 php 中的文本文件中获得以下结果:
[{"name":"Relojes 2018 Watch Men LIGE Fashion Sport","price":"US $18.19"},{"name":"Relojes 2018 Watch Men LIGE Fashion Sport","price":"US $18.19"},{"name":"Relojes 2018 Watch Men LIGE Fashion Sport","price":"US $18.19"}]
在android studio中,我将一些代码格式化为json,然后在字符串中为:
private void postData() {
try {
String postReceiverUrl = "http://192.168.1.104/bot/post.php";
Log.v(TAG, "postURL: " + postReceiverUrl);
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(postReceiverUrl);
List<NameValuePair> nameValuePairs = new ArrayList();
DatabaseHandler db = new DatabaseHandler(this);
db.openDB();
Cursor c = db.getAllDatas();
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
while (c.moveToNext()) {
String name = c.getString(1);
String price = c.getString(2);
//nameValuePairs.add(new BasicNameValuePair("name", name));
//nameValuePairs.add(new BasicNameValuePair("price", price));
jsonObject.put("name", name);
jsonObject.put("price", price);
jsonArray.put(jsonObject);
//httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//HttpResponse response = httpClient.execute(httpPost);
}
//System.out.println("jsonObj" + jsonArray.toString());
//StringEntity se = new StringEntity( jsonObject.toString());
//se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
//httpPost.setEntity(se);
nameValuePairs.add(new BasicNameValuePair("data", jsonArray.toString()));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
在我的接收器 php 代码中:
<?php
$name=$_POST["data"];
//$price = $_POST["price"];
//$result = '<br>' . $name . ' ' . $price;
$result = $name;
$filename="submitted-msg.txt";
file_put_contents($filename,$result,FILE_APPEND);
$androidmessages=file_get_contents($filename);
echo $androidmessages;
?>
现在我的疑问是,我可以像以前一样将字符串转换为 json 数组吗?或者可以在php中做这样的事情吗?要做,怎么做?提前致谢!
解决方案
使用带有 'JSON_UNESCAPED_SLASHES' 标志的 json_encode 来删除反斜杠。
json_encode($androidmessages, JSON_UNESCAPED_SLASHES);
或者你也可以使用stripslashes
stripslashes(json_encode($androidmessages));
您将获得响应 json 格式。
[{"name":"Relojes 2018 Watch Men LIGE Fashion Sport","price":"US $18.19"},{"name":"Relojes 2018 Watch Men LIGE Fashion Sport","price":"US $18.19"},{"name":"Relojes 2018 Watch Men LIGE Fashion Sport","price":"US $18.19"}]
推荐阅读
- javascript - 搜索查询参数 Javascript
- python - 如果我试图拟合来自 pandas 数据帧的数据,如何找到卷积神经网络的 input_shape?
- jquery - 如何在 jQuery 中获取 oauth 令牌
- c# - 为什么 C# 中的 32 位移位会返回它最初移位的值?
- ios - Swift - 为什么在调试视图层次结构中看不到 CALayer 图像
- python - 如何为从 1 到 100 万 Python 的 2 的所有幂编写 For 循环
- php - 使用 jquery ajax 将数据发布到数据库时遇到问题
- excel - Visual Basic Windows Shell 在 2 个不同的目录中启动,具体取决于用户
- r - 使用 ggplot2 和 ggpattern 将图像添加到 R 图 - 图像丢失?
- azure - 获取 Log Analytics 主键