search

Home  >  Q&A  >  body text

How to convert json string to json object in php

Seeking
Similar to js

msg = eval('(' msg ')');

How to write

Currently we know that php uses json_decode() for string json and returns null, and Baidu has no other methods

代言代言2773 days ago949

reply all(2)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-30 09:58:25

    <?php
    $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
    
    var_dump(json_decode($json));
    var_dump(json_decode($json, true));
    
    ?>

    The results are

    object(stdClass)#1 (5) {
        ["a"] => int(1)
        ["b"] => int(2)
        ["c"] => int(3)
        ["d"] => int(4)
        ["e"] => int(5)
    }
    
    array(5) {
        ["a"] => int(1)
        ["b"] => int(2)
        ["c"] => int(3)
        ["d"] => int(4)
        ["e"] => int(5)
    }

    If you return null after json_decode, have you written the string like this"{ 'bar': 'baz' }", this can be parsed into JSON normally in JS, but it must be written as ' in PHP { "bar": "baz" }', attributes and values ​​must be in double quotes

    reply
    0
  • 世界只因有你

    世界只因有你2017-06-30 09:58:25

    Use json_decode(), the premise is that the json format attributes and values ​​must use double quotes. If you use python, you don’t need to use double quotes

    reply
    0
  • Cancelreply