0

A have to send this Json Array from Android to a php script. In this case I've send this json with 1 element ('CABECERA') and in my php script I don't know how to parse to work with.

How I have to do to re-create an entire CABECERA object from this json?.

 $_jsone_str= [ {\"CABECERA\":[{\"CustomerID\":\"1\",\"datetime\":\"\",\"fecha\":\"150303122830\",\"idadmin\":\"3\",\"idcli\":\"4\",\"msj\":\"\",\"nroped\":\"\",\"orderId\":\"1\",\"puntoVentaID\":\"AMALGAME\",\"status\":\"0\",\"total\":\"0.0\"}]}]

$json = json_decode($_jsone_str);

foreach ( $json ->CABECERA as $decode ){
   print_r($decode);
}

How supose to parse this json array what I do wrong?

1
  • It's json_decode() what you are looking? | PHP >= 5.2.0 Commented Mar 3, 2015 at 17:51

3 Answers 3

1

What I usually do is the following:

  1. I first check if POST JSON with POST HEADER POST exists:

    if( isset($_POST["POST"]) ) {
    
    }
    
  2. I deallocate the JSON file:

    $data = $_POST["JSON"];
    $data = stripslashes($data);
    $jsonDecoded = json_decode($data);
    
  3. I then parse the JSON data:

    foreach ($jsonDecoded->**"object/array name"** as $object) {
    
    }
    

In your case, "object/array name" happens to be CABECERA

Full code:

if( isset($_POST["JSON"]) ) {

    $data = $_POST["JSON"];
    $data = stripslashes($data);
    $jsonDecoded = json_decode($data);

    foreach ($jsonDecoded->**"object/array name"** as $object) {

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

Comments

0

The json array must be a string.

And the function json_decode($data, true) - Lookup for the second paremeter, it will return the parsed json in associative arrays else it will be as object.

    $json = "[ {\"CABECERA\":[{\"CustomerID\":\"1\",\"datetime\":\"\",\"fecha\":\"150303122830\",\"idadmin\":\"3\",\"idcli\":\"4\",\"msj\":\"\",\"nroped\":\"\",\"orderId\":\"1\",\"puntoVentaID\":\"AMALGAME\",\"status\":\"0\",\"total\":\"0.0\"}]}]";

foreach ( json_decode($json, true) as $decode ){
   print_r($decode);
}

3 Comments

I just did a test on the code and it worked. What is your php version?
How I have to do to re-create an entire CABECERA object from this json?.
You must parse the string with json_decode(), if your php version is lower than 5.2 then you must lookup for some 3rd partie json parser or create one.
0

Make sure JSON to be decoded is a string:

$_jsone_str= "[ {\"CABECERA\":[{\"CustomerID\":\"1\",\"datetime\":\"\",\"fecha\":\"150303122830\",\"idadmin\":\"3\",\"idcli\":\"4\",\"msj\":\"\",\"nroped\":\"\",\"orderId\":\"1\",\"puntoVentaID\":\"AMALGAME\",\"status\":\"0\",\"total\":\"0.0\"}]}]";

$json = json_decode($_jsone_str);

Check the result:

print_r($json);

Call it the right way:

foreach ( $json as $decode ){
   print_r($decode->CABECERA);
}

Comments

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.