How to convert JSON string to array

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

    If you ever need to convert JSON file or structures to PHP-style arrays, with all the nesting levels, you can use this function. First, you must json_decode($yourJSONdata) and then pass it to this function. It will output to your browser window (or console) the correct PHP styled arrays.

    https://github.com/mobsted/jsontophparray

    0 讨论(0)
  • 2020-11-22 04:18

    this my solution: json string $columns_validation = string(1736) "[{"colId":"N_ni","hide":true,"aggFunc":null,"width":136,"pivotIndex":null,"pinned":null,"rowGroupIndex":null},{"colId":"J_2_fait","hide":true,"aggFunc":null,"width":67,"pivotIndex":null,"pinned":null,"rowGroupIndex":null}]"

    so i use json_decode twice like that :

    $js_column_validation = json_decode($columns_validation);
    $js_column_validation = json_decode($js_column_validation); 
    
    var_dump($js_column_validation);
    

    and the result is :

     array(15) { [0]=> object(stdClass)#23 (7) { ["colId"]=> string(4) "N_ni" ["hide"]=> bool(true) ["aggFunc"]=> NULL ["width"]=> int(136) ["pivotIndex"]=> NULL ["pinned"]=> NULL ["rowGroupIndex"]=> NULL } [1]=> object(stdClass)#2130 (7) { ["colId"]=> string(8) "J_2_fait" ["hide"]=> bool(true) ["aggFunc"]=> NULL ["width"]=> int(67) ["pivotIndex"]=> NULL ["pinned"]=> NULL ["rowGroupIndex"]=> NULL }
    
    0 讨论(0)
  • 2020-11-22 04:19

    You can convert json Object into Array & String.

    $data='{"resultList":[{"id":"1839","displayName":"Analytics","subLine":""},{"id":"1015","displayName":"Automation","subLine":""},{"id":"1084","displayName":"Aviation","subLine":""},{"id":"554","displayName":"Apparel","subLine":""},{"id":"875","displayName":"Aerospace","subLine":""},{"id":"1990","displayName":"Account Reconciliation","subLine":""},{"id":"3657","displayName":"Android","subLine":""},{"id":"1262","displayName":"Apache","subLine":""},{"id":"1440","displayName":"Acting","subLine":""},{"id":"710","displayName":"Aircraft","subLine":""},{"id":"12187","displayName":"AAC","subLine":""}, {"id":"20365","displayName":"AAT","subLine":""}, {"id":"7849","displayName":"AAP","subLine":""}, {"id":"20511","displayName":"AACR2","subLine":""}, {"id":"28585","displayName":"AASHTO","subLine":""}, {"id":"45191","displayName":"AAMS","subLine":""}]}';
    
    $b=json_decode($data);
    
    $i=0;
    while($b->{'resultList'}[$i])
    {
        print_r($b->{'resultList'}[$i]->{'displayName'});
        echo "<br />";
        $i++;
    }
    
    0 讨论(0)
  • 2020-11-22 04:23

    Make sure that the string is in the following JSON format which is something like this:

    {"result":"success","testid":"1"} (with " ") .
    

    If not, then you can add "responsetype => json" in your request params.

    Then use json_decode($response,true) to convert it into an array.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-11-22 04:27

    Try this:

    $data = json_decode($your_json_string, TRUE);
    

    the second parameter will make decoded json string into an associative arrays.

    0 讨论(0)
提交回复
热议问题