【v10】反序列化指定字段
如何才能反序列化指定字段?{
"success": true,
"message": "操作成功!",
"code": 200,
"result": {
"records": [
{
"test1": "",
"test2": "2024",
"test3": "",
"test4": 0,
"test5": "20240301",
.......
}
],
"total": 57,
"size": 20,
"current": 1,
"pages": 3
},
}
我如何反序列化records中的 "test1": "", "test2": "2024",字段,得到对象只包含这两个字段
{
"test1": "",
"test2": "2024"
}
本帖最后由 VVANVI 于 2024-6-26 17:36 编辑
var txt = `
{
"success": true,
"message": "操作成功!",
"code": 200,
"result": {
"records": [
{
"test1": "",
"test2": "2024",
"test3": "",
"test4": 0,
"test5": "20240301"
}
],
"total": 57,
"size": 20,
"current": 1,
"pages": 3
}
}
`
function cl_txt(txt){
var res = txt.split("size=")
num = res.indexOf("]")+1
var json_str =res.substring(num,res.length)
var obj = JSON.parse(json_str)
var arr =obj.result.records
data = []
for (i in arr){
data['test1'] =arr.test1
data['test2'] =arr.test2
}
// console.log(data)
return data
}
console.log(cl_txt(txt))貌似撸代码可以搞, 能接受吗? 大佬,我理解我们整体反序列化之后,用对象操作新建一个对象,然后把这两个属性单独拿出来放在新对象中就可以了呀~~
页:
[1]