Bineesh Kumar
Bineesh Kumar

Reputation: 166

JSON iteration in php using data sent by java/android

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.

Upvotes: 0

Views: 59

Answers (3)

Joni
Joni

Reputation: 111269

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/

Upvotes: 1

Aashit Shah
Aashit Shah

Reputation: 578

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.

Upvotes: 1

GNassro
GNassro

Reputation: 1071

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);

Upvotes: 0

Related Questions