How to convert JSON string to array

后端 未结 14 908
猫巷女王i
猫巷女王i 2020-11-22 03:37

What I want to do is the following:

  1. taking JSON as input from text area in php
  2. use this input and convert it to JSON and pass it to php curl to send
14条回答
  •  灰色年华
    2020-11-22 04:10

    If you are getting the JSON string from the form using $_REQUEST, $_GET, or $_POST the you will need to use the function html_entity_decode(). I didn't realize this until I did a var_dump of what was in the request vs. what I copied into and echo statement and noticed the request string was much larger.

    Correct Way:

    $jsonText = $_REQUEST['myJSON'];
    $decodedText = html_entity_decode($jsonText);
    $myArray = json_decode($decodedText, true);
    

    With Errors:

    $jsonText = $_REQUEST['myJSON'];
    $myArray = json_decode($jsonText, true);
    echo json_last_error(); //Returns 4 - Syntax error;
    

提交回复
热议问题