案例一:
{key:value,key:value}
class Person{ String firstname = "张"; String lastname = "三丰"; Integer age = 100; } Person p = new Person(); System.out.println(p.firstname);
转JS:
var person = {"firstname":"张","lastname":"三丰","age":100}; //取出lastname alert(person.lastname); //取出age alert(person.age);
案例二:
[{key:value,key:value},{key:value,key:value}]
Person[] persons = new Person[1]; Person p0 = new Person("张","三丰",100); Person p1 = new Person("李","四",25); persons[0] = p0; persons[1] = p1; System.out.println(persons[0].age); System.out.println(persons[1].firstname);
转JS:
var persons = [ {"firstname":"张","lastname":"三丰","age":100}, {"firstname":"李","lastname":"四","age":25} ]; //取出fistname=李 alert(persons[1].firstname); //取出age=100 alert(persons[0].age);
案例三:
{
"param":[{key:value,key:value},{key:value,key:value}]
}
var json = { "baobao":[ {"name":"yg",age:22,"addr":"南通"}, {"name":"gy",age:22,"addr":"徐州"} ] }; //取name = gy alert(json.baobao[1].name); //取addr = 南通 alert(json.baobao[0].addr);
案例四:
{
"param1":[{key:value,key:value},{key:value,key:value}],
"param2":[{key:value,key:value},{key:value,key:value}],
"param3":[{key:value,key:value},{key:value,key:value}]
}
var json = { "baobao":[ {"name":"yg",age:22,"addr":"南通"}, {"name":"gy",age:22,"addr":"徐州"} ], "haohao":[{"name":"xh",age:23,"addr":"南京"}, {"name":"aj",age:22,"addr":"淮阴"} ] }; //取name = gy alert(json.baobao[1].name); //取addr = 淮阴 alert(json.haohao[1].addr);
案例五:
{
"param1":"value1",
"param2":{},
"param3":[{key:value,key:value},{key:value,key:value}]
}
var json = { "key1":"value1", "key2":{"firstname":"张","lastname":"三丰","age":100}, "key3":[ {"name":"yg",age:22,"addr":"南通"}, {"name":"gy",age:22,"addr":"徐州"} ] }; alert(json.key2.firstname); alert(json.key3[1].age);