1

I try send string to controller, the string is json format, when send to controller, i get error and can't decode my json string in that controller. I try to encode first in my controller, but still get error. And the error is

"json_decode() expects parameter 1 to be string, array given", exception: "ErrorException",

here in my json string

"{ "data" : 
[{
"id": "TNI01",
 "jenis_bayar": "TUNAI",
"no_kartu": "kosong",
"nominal": "10000",
"seq": "1"
} , 
{
"id": "DEB01",
"jenis_bayar": "DEBIT BCA",
"no_kartu": "786382432432",
"nominal": "20000",
"seq": "2"
}]
}"

here the controller

public function ArrayPostToTablePembayaran(Request $request)
    {

       $data = json_decode($request->datajson, true);

       foreach ($data->data as $datas) 
       {
          $id         = $datas->id;
          $jenisbayar = $datas->jenis_bayar;
          $nokartu    = "";

          if($datas->no_kartu == "kosong")
          {
              $nokartu ="";
          }

          $nominal    = $datas->nominal;
          $seq        = $data->seq;
          $this->PosToTablePembayaran1($id , $jenisbayar , $nokartu , $nominal , $seq); 
       }
    }

and here the ajax script for parse json string to controller

function PembayaranKeDatabase1(arraystring)
            {
              $.ajax(
                  {
                      type    : "POST",
                      url     : "{{ url('/trx_bayar') }}",
                      data    : { datajson : JSON.parse(arraydata) } ,
                      dataType: "json",
                      success: function(data){

                      },
                      error: function() {

                      }
                  });
            }

thanks before

2
  • 1
    So, what is unclear? You pass array instead of string. $request->datajson is array already and you don't need to decode it. Commented May 12, 2019 at 13:37
  • So, I think passing json object to controller? if the string already to be array object, how to make that in foreach ? Commented May 12, 2019 at 13:43

1 Answer 1

1

The main issue in your code that you try to decode json twice: in client js code and on server.

Let's inspect what you do:

JS function PembayaranKeDatabase1(arraystring) has an argument of type string, I presume. I also presume that arraystring is a JSON-string. So, you decode JSON-string to object with

JSON.parse(arraydata)  
// btw shouldn't it be 
//JSON.parse(arraystring)

So, here you send some plain object to server, not json.

Next, on server you try to decode again. But you receive an array in $request->datajson, as json is already decoded on client-side.

So, you can choose between two options:

  1. Remove JSON.parse:

    data    : { datajson : arraydata },
    

and use json_decode on server.

  1. Remove json_decode($request->datajson, true) on server. Iterate over your data as

    // as $request->datajson is an array
    foreach ($request->datajson['data'] as $datas) {
        // use [] notation as you work with array, not object
        echo $datas['id'];
    }
    
Sign up to request clarification or add additional context in comments.

3 Comments

if i choose first option, is the foreach script that I used before correct?
Correct, but option true in json_decode decodes data to array and you'll need [] notation instead of ->. If you still need object and -> - remove second argument of json_decode.
I thinks if i use json format, must decode in controller/server, but that already to be array format, thanks,

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.