0

From my android my PHP sever is receiving the below json :

{
  "class":"SURESHOT",
  "subject":"Maths",
  "qstn":"[607421_15958169393.JPG, 410816_15958169444.JPG, 
          655502_15958169495.JPG,   625421_15958179086.JPG, 
          625421_15958179086.JPG, 461984_15958180457.JPG]",
  "ans":"[C, B, A, D, C, C]",
  "lickey":"jg2ler3xvbdgsjkru12tutujghgjgl4jkjojuir8uttzcdadasretf",
  "user":"1000",
  "result":"[fail,fail, pass, fail, fail, fail]",
  "qid":"[37, 38, 39, 40, 40, 41]"
 }

Now iterating through the data is not possible in PHP. 'qstn', 'ans' and 'result' are the problematic ones. How can we do it properly?

I used json_encode(($_POST),true); for preliminary data conversion.

This is the code I run to get the JSON:

This is the code from I get json.

   reqPostanswers = new StringRequest(Request.Method.POST, urll,new  Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //Log.i("posting info :",response.toString());
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //Log.i("posting error  :",error.toString());
        }
    }){

        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put("subject",subject);
            params.put("class",qSet[qsetCntr][0]);
            params.put("user", thisuser);
            params.put("result",resltn.toString());
            params.put("qstn",qstnn.toString());
            params.put("qid", qiddn.toString());
            params.put("ans",answn.toString());
            params.put("lickey","jg2ler3xvbdgsjkru12tutujghgjgl4jkjojuir8uttzcdadasretf");

            return params;
        }

    };
    answerpostQueue = Volley.newRequestQueue(getApplicationContext());
    answerpostQueue.add(reqPostanswers);

The arrays or variables in the prams set up is Arraylists only.

2
  • 2
    Can you show the code the creates this problematic JSON? Commented Aug 9, 2020 at 18:22
  • @Joni, I hve added code. Commented Aug 9, 2020 at 18:49

3 Answers 3

1

The Android code is not actually sending JSON to the server. It's using the old key-value form field format, which does not have a way to represent complex objects. To fix this, change the Android code to use JsonObjectRequest instead of StringRequest, and pass the request in as a JSONObject:

JSONObject params = new JSONObject();
params.put("subject",subject);
params.put("class",qSet[qsetCntr][0]);
params.put("user", thisuser);
params.put("result", new JSONArray(resltn));
params.put("qstn", new JSONArray(qstnn));
params.put("qid", new JSONArray(qiddn));
params.put("ans", new JSONArray(answn));
params.put("lickey","jg2ler3xvbdgsjkru12tutujghgjgl4jkjojuir8uttzcdadasretf");

reqPostanswers = new JsonObjectRequest(Request.Method.POST, urll, params,
    new  Response.Listener<String>() { ... },
    new Response.ErrorListener() { ... }
});

After you have done this change, you most likely have to change your PHP code too, so that it reads JSON from the request body. How to do that is explained for example here: https://www.geeksforgeeks.org/how-to-receive-json-post-with-php/

Sign up to request clarification or add additional context in comments.

2 Comments

you did it. Thanks. It was a great help. Still the whole issue is not fixed.
This was the php part i had to use for success. $json = file_get_contents('php://input'); $decoded=json_decode($json,true); so that $decoded is a qualified php array.
1

first of all the problem is with your created json. Json array should be as shown below ,

"qstn": ["607421_15958169393.JPG","410816_15958169444.JPG",
      "655502_15958169495.JPG","625421_15958179086.JPG","625421_15958179086.JPG", 
"461984_15958180457.JPG"]

Convert your json array in the manner shown above and in php side use ,

$model = json_decode($json,true);

Now u can access the values from this variable as following:

 $class = $model["class"];
 //The 0th index of qstn array can be accessed as
 $qsnt1 = $model["qstn"][0];

For saving yourself from such errors in future use gson to create json for you.

Comments

0

using ltrim and rtrim functions to delete the [ and ] from the string, then with explode you can convert the string into array. NB: this exemple only work when the delimiter is , and blank space (", ")

exemple with "qstn":

$array = json_decode($json, true);

$resultArray = explode(", ",rtrim(ltrim($array["qstn"],"["),"]"));

var_dump($resultArray);

3 Comments

thank you. But what is the best way to create complex JSON from java arrays ?
i didn't undrestand what you mean create complex JSON from java arrays ?
my question was not a qualified one. Sorry.

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.