How to convert JSON string to array

后端 未结 14 901
猫巷女王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:26

    Use this convertor , It doesn't fail at all: Services_Json

    // create a new instance of Services_JSON
    $json = new Services_JSON();
    
    // convert a complexe value to JSON notation, and send it to the browser
    $value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4)));
    $output = $json->encode($value);
    print($output);
    // prints: ["foo","bar",[1,2,"baz"],[3,[4]]]
    
    // accept incoming POST data, assumed to be in JSON notation
    $input = file_get_contents('php://input', 1000000);
    $value = $json->decode($input);
    
    // if you want to convert json to php arrays:
    $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
    

提交回复
热议问题