Most of the time, we need to assemble a string as a JSON object. First, we need to combine the string and then convert it into a JSON object. The following example shows how:
< Script Type = " Text/JavaScript " >
<! --
VaR A = 50 , B = " Xxx " ;
VaR Arr = " {ID: " + A + " , Name :' " + B + " '} " ;
// -->
</ Script >
Combined into the string arr, the next step is to convert it into an object, and soon we will think of using the eval method, but if this conversion will produce an error, today I will try it like this, how can we convert it into a JSON object? After being depressed for a long time, I found a solution in the JSON. js file provided on the JSON official website. The solution is as follows:
Add parentheses at both ends of the string and then the eval will be OK. TestCodeAs follows:
< Script Type = " Text/JavaScript " >
<! --
VaR A = 50 , B = " Xxx " ;
VaR Arr = " {ID: " + A + " , Name :' " + B + " '} " ;
Arr = Eval ('(' + Arr + ')')
Alert (ARR. Name );
// -->
</ Script >