本篇文章為大家帶來了關於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格式的數據接收
b4b38e33757a6497aa8690936b905cc1 05a8acc5c31084a4f61ade01873802cacom.fasterxml.jackson.core192ca2f7b8c770b01c8f81e6bdd5b947 9bc4cbb67f66e148869423c0d27e5f90jackson-databindb68fb17cb904a46b73e6272850323873 3d689bd3819ead35ed794427bd12f4592.8.383a577b3f930c490b31329be5e672d0b 09a0e22e5aaafd848ae04665be625b91
//类属性, private static final ObjectMapper MAPPER = new ObjectMapper(); //序列化-----userMapList是Listbe1cdaf6779910b92a0a47fc24e82b4f的格式,现在我们需要将List集合序列化为json字符串 MAPPER.writeValueAsString(userMapList); //反序列化---json格式的字符串要反序列化为对象 MAPPER.readValue(你要反序列化的json字符串,new TypeReference5ffa1bc620359d13a5b754cd7e8e7c40() {});
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}JSON.stringify()中的第三個參數表示縮排的空格數,這樣傳送的資料可讀性比較好。
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中文網其他相關文章!