search

Home  >  Q&A  >  body text

将How to retrieve and display JSON content in a WordPress function using PHP?

I have a wordpress endpoint and have some json data. Unfortunately I don't know how to return this json data in the function. I tried json_decode but it returned nothing. Then the endpoint will work. If I use json_encode it returns the data but also includes newlines and other stuff. The problem seems to be with the syntax, since it's already a complete json that I have. How to return something already in json syntax?

add_action('wp_ajax_nopriv_inboundCall', 'testFunction');
add_action('wp_ajax_inboundCall', 'testFunction');
 
function testFunction() {
    
    echo json_decode('{
        "testData": [
          {
            "_id": "1",
            "name": "testName1"
          },
          {
            "_id": "2",
            "name": "testName2"
          },
        ],
        "testState": {
          "1": [
            1,
            0
          "2": [
            1,
            0
          ]
         }
       }'); 

      die(); 
}

P粉731861241P粉731861241228 days ago403

reply all(1)I'll reply

  • P粉980815259

    P粉9808152592024-03-31 00:38:57

    function testFunction() {
    return json_decode('{
        "testData": [
          {
            "_id": "1",
            "name": "testName1"
          },
          {
            "_id": "2",
            "name": "testName2"
          },
        ],
        "testState": {
          "1": [
            1,
            0
          "2": [
            1,
            0
          ]
         }
       }');  }

    reply
    0
  • Cancelreply