{ "user": {
"name": "李四",
"preferences": {
"theme": "dark",
"notifications": true,
"fontSize": 14.5
},
"recentSearches": ["编程教程", "JSON解析", "API设计"]
} }
const userData = { id: 12345, name: '王小明', lastLogin: new Date(), settings: { theme: 'dark', notifications: true } };
// 序列化时处理日期对象 const jsonString = JSON.stringify(userData, (key, value) => { if (value instanceof Date) {
return value.toISOString();
} return value; }, 2);
{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "required": ["username", "email", "password"], "properties": {
"username": {
"type": "string",
"minLength": 3,
"maxLength": 20,
"pattern": "^[a-zA-Z0-9_]+$"
},
"email": {
"type": "string",
"format": "email"
},
"password": {
"type": "string",
"minLength": 8,
"pattern": "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).+$"
},
"age": {
"type": "integer",
"minimum": 0,
"maximum": 150
}
} }
import json data = json.loads('{"name": "John", "age": 30}') json_string = json.dumps(data, indent=2)