packagemain
import(
"encoding/json"
"fmt"
"github.com/robertkrimen/otto"
)
var testJsonstring=`{
"name":"tim",
"age":10,
"sites":["Google","Runoob","Taobao"],
"memo":{"info":"淘宝"}
}`
funcmain(){
varmapStrmap[string]interface{}
json.Unmarshal([]byte(testJson),&mapStr)
fmt.Println(mapStr["sites"])
vm:=otto.New()
vm.Set("mapStr",mapStr)
vm.Set("sites",mapStr["sites"])
vm.Set("memo",mapStr["memo"])
srcScript:=`
A=100;
B=200;
C=100;
console.log("Thesiteslen"+sites.length);
console.log("Thememoinfo"+memo.info);
console.log("ThemapStr.memo.infois"+mapStr.memo.info);
varid=Math.random();
`
_,err:=vm.Run(srcScript)
iferr!=nil{
fmt.Println(err.Error())
}
v,_:=vm.Get("memo")
v1,_:=v.Export()
v2,_:=v1.(map[string]interface{})
fmt.Println(v2["info"])
}
网友评论