0

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!

2
  • really dont understand what do you want to achieve. what i get, you convert your dbvalues to json and then send it to server and now you want to get that json and convert it back into dbvalues ? Commented Jul 13, 2019 at 5:26
  • @faiizii i want to parse the converted string into json again, in php Commented Jul 13, 2019 at 14:12

2 Answers 2

1

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"}]
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

echo json_encode($androidmessages);

1 Comment

Tried it, showing something like, "[{\"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\"}]"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.