简体   繁体   中英

Converting JSON array to String and JSON again android

I am sending some post data to my local php server that i'd created. I wanted the post data to be in json format so that pursing those later would be easy from php. Using Namevaluepairs , i converted my jsonArray to a String and getting the following results in php, in a text file:

[{"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:

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

$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? Or is it possible to do something like this in php? To do, how so? Thanks in advance!

use json_encode with 'JSON_UNESCAPED_SLASHES' flag to remove backslashes.

json_encode($androidmessages, JSON_UNESCAPED_SLASHES);

Or you can also use stripslashes

stripslashes(json_encode($androidmessages));

you will get the response json format.

[{"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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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