turn string data into json format

This question already has an answer here:

  • Safely turning a JSON string into an object 22 answers

  • If that's the string then I'll suggest to wrap it in { ... } and use `JSON.parse. Ie:

    var json = JSON.parse('{' + string + '}');
    

    Of course you will need to add JSON lib helper to your page https://github.com/douglascrockford/JSON-js


    你可以使用JSON.parse(string)这将返回一个JSON字符串


    I think you have a problem with PHP not with Javascript:

    you have decoded a JSON string for trasform it in a PHP object with json_decode work with it... but now you must get back it in a well format JSON string.

    But first your string is invalid, for JSON standard (in PHP):

  • Enclose your string in {
  • the name of the properties note, quote, project, skill must be encapsuled inside " , your new string:

    { "note":{"category_id":"1","name":"Notes","icon":"images/note.png"}, "quote":{"category_id":"2","name":"Quotes","icon":"images/quote.png"}, "project"{"category_id":"3","name":"Projects","icon":"images/project.png"}, "skill":{"category_id":"4","name":"Skills","icon":"images/skill.png"} }

  • and now see this example of JSON encodingi in PHP:

    $yourString = '{"note":{"category_id":"1","name":"Notes","icon":"images/note.png"},"quote":{"category_id":"2","name":"Quotes","icon":"images/quote.png"},"project":{"category_id":"3","name":"Projects","icon":"images/project.png"},"skill":{"category_id":"4","name":"Skills","icon":"images/skill.png"}}'; 
    $JSON_FOR_PHP = json_decode($yourString);
    $JSON_FOR_JS = json_encode($JSON_FOR_PHP);
    
    /* response: */
    echo "JSON for PHP (associative Array):<br><br>";
    var_dump($JSON_FOR_PHP);
    echo"<br><br>";
    echo "JSON for JAVASCRIPT (JSON string {add content type: application/json}):<br><br>";
    echo $JSON_FOR_JS;
    

    response:

    JSON for PHP (associative Array):
    
    object(stdClass)#1 (4) { ["note"]=> object(stdClass)#2 (3) { ["category_id"]=> string(1) "1" ["name"]=> string(5) "Notes" ["icon"]=> string(15) "images/note.png" } ["quote"]=> object(stdClass)#3 (3) { ["category_id"]=> string(1) "2" ["name"]=> string(6) "Quotes" ["icon"]=> string(16) "images/quote.png" } ["project"]=> object(stdClass)#4 (3) { ["category_id"]=> string(1) "3" ["name"]=> string(8) "Projects" ["icon"]=> string(18) "images/project.png" } ["skill"]=> object(stdClass)#5 (3) { ["category_id"]=> string(1) "4" ["name"]=> string(6) "Skills" ["icon"]=> string(16) "images/skill.png" } }
    
    JSON for JAVASCRIPT (JSON string {add content type: text/json}):
    
    {"note":{"category_id":"1","name":"Notes","icon":"images/note.png"},"quote":{"category_id":"2","name":"Quotes","icon":"images/quote.png"},"project":{"category_id":"3","name":"Projects","icon":"images/project.png"},"skill":{"category_id":"4","name":"Skills","icon":"images/skill.png"}}
    

    if you echo only $JSON_FOR_JS and change content type to application/json you get the response is a valid JSON string that you can parse with JSON.parse() in javascript:

    header('Content-Type: application/json');
    echo $JSON_FOR_JS;
    

    or echo it directly in a JS script (html page or without script tag in .js file):

    var js_json = JSON.parse();

    now in javascript you have a object js_json with the content of your string.

    链接地址: http://www.djcxy.com/p/31712.html

    上一篇: 从AJAX响应中获取值

    下一篇: 将字符串数据转换为json格式