Eval question in javascript

Thank you. here is the correct question:

{
    "VID":"60",
    "name":"\u4f1a\u9634",
    "requireLevel":"20",
    "levelMax":"5",
    "venationRequirement":"0",
    "description":"\u6c14\u6d77\u4e0a\u9650\u63d0\u9ad8[Affect1]\u70b9",
    "cost":{"1":"240","2":"360","3":"400","4":"600","5":"720"},
    "difficult":{"1":"1024","2":"973","3":"921","4":"870","5":"819"},
    "affect":{"1":"200","2":"500","3":"900","4":"1400","5":"2000"},
    "descriptions":{
        "1":"\u6c14\u6d77\u4e0a\u9650\u63d0\u9ad8200\u70b9",
        "2":"\u6c14\u6d77\u4e0a\u9650\u63d0\u9ad8500\u70b9",
        "3":"\u6c14\u6d77\u4e0a\u9650\u63d0\u9ad8900\u70b9",
        "4":"\u6c14\u6d77\u4e0a\u9650\u63d0\u9ad81400\u70b9",
        "5":"\u6c14\u6d77\u4e0a\u9650\u63d0\u9ad82000\u70b9"
    }
}

      

I have used json_encode () in php and ajax request to get the response body.

but when i use eval () to parse the response text. it is not right.

moonshadow and james gregory have answered this question in the comments below. Again.

+1


source to share


5 answers


Your second is wrong because you are evaluating the hash, since it works, you need to rewrite it as something like:



var s = '{"first": {"a":1}, "second": {"b":2}}';

      

+4


source


Javascript is a little confused about what context it parses in. Prepare '(' and add ')' before passing the string to eval () to make it parse the whole thing as an expression.



(Your asked question is also missing labels for the outer associative array as others have pointed out, however the data samples you provided clarify the actual problem.)

+3


source


If your second example is trying to create an object with two nested objects, you are missing the names for the properties of the outer object, eg.

var s = '{"FirstSubObject": {"a": 1}, "SecondSubObject": {"b": 2}}';

+2


source


The syntax [] is clearly meant for creating arrays, whereas {} has a completely different meaning (it thinks you are trying to create a function / block)

0


source


Using json is a much better solution for this.

0


source







All Articles