简体   繁体   English

再次将JSON数组转换为String和JSON

[英]Converting JSON array to String and JSON again android

I am sending some post data to my local php server that i'd created. 我正在将一些发布数据发送到我创建的本地php服务器。 I wanted the post data to be in json format so that pursing those later would be easy from php. 我希望发布数据为json格式,以便以后从php轻松进行提取。 Using Namevaluepairs , i converted my jsonArray to a String and getting the following results in php, in a text file: 使用Namevaluepairs ,我将jsonArray转换为String并在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"}]

In android studio i had some code formatting things into json then in a string as: 在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();
        }
    }

And in my receiver php code: 在我的接收器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;

?>

Now my doubt is, can i convert the string into json array as i wanted before? 现在我的疑问是,我可以像以前一样将字符串转换为json数组吗? Or is it possible to do something like this in php? 或者有可能在php中做类似的事情? To do, how so? 要这样做,怎么做? Thanks in advance! 提前致谢!

use json_encode with 'JSON_UNESCAPED_SLASHES' flag to remove backslashes. 使用带有“ JSON_UNESCAPED_SLASHES”标志的json_encode来删除反斜杠。

json_encode($androidmessages, JSON_UNESCAPED_SLASHES);

Or you can also use stripslashes 或者您也可以使用反斜杠

stripslashes(json_encode($androidmessages));

you will get the response json format. 您将获得响应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"}]

尝试这个:

echo json_encode($androidmessages);

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

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