How to convert JSON string to array

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

    your string should be in the following format:

    $str = '{"action": "create","record": {"type": "n$product","fields": {"n$name": "Bread","n$price": 2.11},"namespaces": { "my.demo": "n" }}}';
    $array = json_decode($str, true);
    
    echo "
    ";
    print_r($array);
    

    Output:

    Array
     (
        [action] => create
        [record] => Array
            (
                [type] => n$product
                [fields] => Array
                    (
                        [n$name] => Bread
                        [n$price] => 2.11
                    )
    
                [namespaces] => Array
                    (
                        [my.demo] => n
                    )
    
            )
    
    )
    

提交回复
热议问题