How would I go about doing this?

后端 未结 3 1458
既然无缘
既然无缘 2021-01-29 12:18

First I have my data encoded in the json_encode function.

Looks like this for example:

{\"test\":\"test value\"}

What I want to do is m

3条回答
  •  长发绾君心
    2021-01-29 12:44

    Objects are associative arrays. They store key/values pairs. So All you have to do is:

    var test = function(){}
    test["hello"] = "world";
    

    This will set hello as a variable and world as its value. you can test this by doing

    alert(test.hello);
    

    Replace hello and world with the json key and value

    Hope this help more with this example: I am using Jquery AJAX to go to index.php resource and return a json object.

    index.php

     'world');
    echo json_encode($variables);
    ?>
    

    example.html

    var test = function(){}
    $.ajax({
       url: index.php,
       success: function(json) {
        for(var key in json ){
         var testVarName = key;
         var testVarValue = json[key];
         test[testVarName ] = testVarValue;
        }
    }
    });
    

    So now test object has variable hello and it value is world

提交回复
热议问题