只要你的风格是适应客观规律的,那你以后会越来越好,做事情会越来越顺利
------ 24.8.26
一、JS创建对象
语法
方式1 new Object()
方式2 {属性名:属性值,... ...,函数名:function(){}}
方式3 Object.create()
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
// 方式1 new Object()
// 方式2 {属性名:属性值,... ...,函数名:function(){}}
// 方式3 Object.create()
var person = new Object();
person.name = "张三";
person.age = 20;
person.eat = function(food){
console.log(this.name + "在吃" + food);
}
// 访问属性
console.log(person.name); // 张三
console.log(person.age); // 20
// 调用函数
person.eat("苹果"); // 张三在吃苹果
var person2 = {
age: 25,
name:"李四",
eat: function(food){
console.log(this.name + "在吃" + food);
}
}
// 访问属性
console.log(person2.name); // 李四
console.log(person2.age); // 25
// 调用函数
person2.eat("西瓜"); // 李四在吃苹果
var person1 = Object.create(null);
person1.name = "王五";
person1.age = 27;
person1.eat = function(food){
console.log(this.name + "在吃" + food);
}
// 访问属性
console.log(person1.age); // 25
console.log(person1.name); // 王五
// 调用函数
person1.eat("香蕉"); // 王五在吃香蕉
</script>
</head>
<body>
</body>
</html>
二、JSON格式
一个特定的字符串语法结构,JSON格式的字符串,在前后端都可以很方便的和对象之间进行转换
说明:
JSON的语法:
javascript
var personStr = '{"属性名":"属性值"}'
var obj="{'属性名':'属性值','属性名':{'属性名':'属性值'}',属性名':{'值1','值2','值3'}"
JSON字符串一般用于传递数据,所以字符串中的函数就显得没有意义,在此不做研究
通过JSON.parse()方法可以将一个JSON串转换成对象
通过JSON.stringify()方法可以将一个对象转换成一个JSON格式的字符串
JSON在客户端的使用
代码:
javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
/*
JSON格式的语法
var personStr = '{"属性名":"属性值","属性名":"属性值","属性名":["属性值"],"属性名":{"属性值"}}'
属性名必须用 "" 包裹
属性值 字符串必须用""包好,数字可以不处理
属性值可以是字符串、数字、布尔值、数组、对象、null
*/
// 这是一个JSON格式的字符串
var personStr = '{"name":"张三","age":25,"dog":{"dname":"初一"},"loveMusic":["愿与愁","你一定能成为你想要成为的人"],"friends":[{"fname":"孙茂榛"},{"fname":"王衍博"},{"fname":"刘宇昕"}]}'
// 通过JSON.parse()方法将JSON字符串转换为一个JS对象
var person = JSON.parse(personStr)
console.log(person)
console.log(typeof person)
console.log(personStr)
console.log(typeof personStr)
console.log(person.name)
console.log(person.dog.dname)
console.log(person.loveMusic[1])
console.log(person.friends[1].fname)
</script>
</head>
<body>
</body>
</html>
JSON在服务端的使用
代码
Dog类
java
package JSON;
import java.util.Objects;
public class Dog {
private String name;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dog dog = (Dog) o;
return Objects.equals(name, dog.name);
}
@Override
public int hashCode() {
return Objects.hashCode(name);
}
public Dog() {
}
public Dog(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Person类
java
package JSON;
import java.util.Objects;
public class Person {
private String name;
private Integer age;
private Dog dog;
public Person() {
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(name, person.name) && Objects.equals(age, person.age) && Objects.equals(dog, person.dog);
}
@Override
public int hashCode() {
return Objects.hash(name, age, dog);
}
public Person(String name, Integer age, Dog dog) {
this.name = name;
this.age = age;
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
}
测试类
java
package TestJson;
import JSON.Dog;
import JSON.Person;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
public class test {
// 字符串转对象
@Test
public void testWriteJson() throws JsonProcessingException {
// 实例化Person对象,将Person对象转换为JSON串
Dog dog = new Dog("初一");
Person person = new Person("张三",13,dog);
// 将Person对象转换成一个字符串 Gson jackson Fastjson 都是转换工具
ObjectMapper mapper = new ObjectMapper();
String personStr = mapper.writeValueAsString(person);
System.out.println(personStr);
}
// 对象转字符串
@Test
public void testReadJson() throws JsonProcessingException {
String personStr = "{\"name\":\"张三\",\"age\":13,\"dog\":{\"name\":\"初一\"}}";
ObjectMapper mapper = new ObjectMapper();
Person person = mapper.readValue(personStr, Person.class);
System.out.println(person);
}
}
三、JSON和Map、List、Array之间的转换
1.map转JSON对象
java
// map转JSON对象
@Test
public void testMapToJason() throws JsonProcessingException {
Map data = new HashMap();
data.put("a","valuea");
data.put("b","valueb");
ObjectMapper mapper = new ObjectMapper();
String s = mapper.writeValueAsString(data);
System.out.println(s);
}
2.List转JSON对象
java
// List转JSON对象
@Test
public void testListToJson() throws JsonProcessingException {
List data = new ArrayList();
data.add("苦");
data.add("难");
data.add("是");
data.add("花");
data.add("开");
data.add("的");
data.add("伏");
data.add("笔");
ObjectMapper mapper = new ObjectMapper();
String s = mapper.writeValueAsString(data);
System.out.println(s);
}
3.List转Array数组
java
// Array转JSON对象
@Test
public void testArrayToJson() throws JsonProcessingException {
String[] data = {"苦","难","是","花","开","的","伏","笔"};
ObjectMapper mapper = new ObjectMapper();
String s = mapper.writeValueAsString(data);
System.out.println(s);
}
同理,元素也可以是对象