JavaScript自定义对象:
var 对象名称 = { 属性名称1:属性值1, 属性名称2:属性值2, ..., 函数名称:function (形参列表){}, ... }; 访问对象:对象名称.属性名称 对象名称.函数名称(形参) json对象: { "name":"zhangsan", "age":23, "city":"北京" } var 变量名 = '{"key":value, "key":value,... }'; var jsonStr = '{"name":"zhangsan", "age":23," addr":["北京","上海","西安"]}' JSON和Java对象的相互转换 :导入包<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency>
:相互转换
//1. 将Java对象转为JSON字符串 User user = new User(); user.setId(1); user.setUsername("zhangsan"); user.setPassword("123"); String jsonString = JSON.toJSONString(user); System.out.println(jsonString);//{"id":1,"password":"123","username":"zhangsan"} //2. 将JSON字符串转为Java对象 User u = JSON.parseObject("{\"id\":1,\"password\":\"123\",\"username\":\"zhangsan\"}", User.class); System.out.println(u);
标签:自定义,对象,JavaScript,User,JSON,user,zhangsan,名称 From: https://www.cnblogs.com/zhaolei0419/p/16721000.html