本篇文章為大家帶來了關於javascript的相關知識,其中主要介紹了關於Json對象的相關問題,JavaScript Object Notation用於存儲和交換文本信息的語法,進行數據的傳輸,JSON比XML 更小、更快,更容易解析,下面一起來看一下,希望對大家有幫助。
【相關推薦:javascript影片教學、web前端】
##JsonJavaScript Object Notation -JavaScriptJson字串用於儲存和交換文字資訊的語法,進行資料的傳輸,JSON比XML 更小、更快,更容易解析。
var person = {"name": "张三", age: 23, 'gender': true};var ps = [{"name": "张三", "age": 23, "gender": true}, {"name": "李四", "age": 24, "gender": true}, {"name": "王五", "age": 25, "gender": false}];登入後複製
var b='{"name":"2323","sex":"afasdf","age":"6262"}';//json字符串 console.log(b);//{"name":"2323","sex":"afasdf","age":"6262"} alert(typeof(b));//string
#將資料結構或是物件轉換為二進位字串(位元組序列)的過程用來資料的傳輸(把資料裝換成json字串後端配合@reponseBody來進行資料的接受與傳遞)
Java物件序列化了才可以在網路上傳輸,或是儲存到硬碟上。
序列化完就變成了json字串了透過jackson這個序列化框架序列化
在controller的請求參數前加@reponseBody 就可以將前端傳過來的json格式的數據接收
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.3</version> </dependency>
//类属性, private static final ObjectMapper MAPPER = new ObjectMapper(); //序列化-----userMapList是List<user>的格式,现在我们需要将List集合序列化为json字符串 MAPPER.writeValueAsString(userMapList); //反序列化---json格式的字符串要反序列化为对象 MAPPER.readValue(你要反序列化的json字符串,new TypeReference<要反序列化为什么对象类型,例如 User.class>() {});
var person = { username: ‘luohao’, password: 123456, location: ‘whu’ }
var json = JSON.stringify(person, [‘username’, ‘password’]); console.log(json); {“username”:“luohao”,“password”:123456}
var person = { username: ‘luohao’, password: 123456, location: ‘whu’, hometown: ‘wuhan’, } var json = JSON.stringify(person, function filter(key, value) { switch(key) { case ‘location’: return undefined; case ‘hometown’: return undefined; default: return value; } }); console.log(json); {“username”:“luohao”,“password”:123456}
var person = { username: ‘luohao’, password: 123456, location: { province: ‘hubei’, city: ‘wuhan’, county: ‘qichun’ }, hometown: ‘wuhan’, } var json = JSON.stringify(person, function filter(key, value) { switch(key) { case ‘hometown’: return undefined; default: return value; } }); console.log(json); {“username”:“luohao”,“password”:123456,“location”:{“province”:“hubei”,“city”:“wuhan”,“county”:“qichun”}}
var person = { username: ‘luohao’, password: 123456, location: { province: ‘hubei’, city: ‘wuhan’, county: ‘qichun’ }, hometown: ‘wuhan’, } var json = JSON.stringify(person, function filter(key, value) { switch(key) { case ‘hometown’: return undefined; default: return value; } }, 2); console.log(json); { “username”: “luohao”, “password”: 123456, “location”: { “province”: “hubei”, “city”: “wuhan”, “county”: “qichun” } }
以上是範例解析Json物件Json字串的序列化與反序列化的詳細內容。更多資訊請關注PHP中文網其他相關文章!