[QML][Json]QML下Json增删查改QML下Json相关
qml json 解析 字符串化
Qml下,提供了JSON对象,以帮助完成json对象的⽣成和字符串化.
⼀.Json对象的⽣成
1.从字符串解析,借助函数 JSON.parse(text[, reviver])
1.1 参数
text为需要解析的字符串
reviver 为可选的函数参数,parse在解析时为,每⼀个对象掉⽤该函数.
1.2 只传⼊text调⽤
var jsonStr="{\"k1\":\"v1\",\"k2\":\"v2\",\"o2\":{\"ok1\":\"ov1\"},\"ak1\":[\"av1\",\"av2\"]}"
var json=JSON.parse(jsonStr)
上述json即为解析到的json对象,可使⽤该对象对json的内容进⾏访问修改的⼀系列操作
1.3 传⼊text 和reviver调⽤(PS:不知道为什么QML下传⼊函数并没有被调⽤或者调⽤失败)
声明⼀个函数,该函数打印对象的key和value
function printJson(k,v){
console.log("k: "+k);
console.log("v: "+v);
}
如下调⽤
var jsonStr="{\"k1\":\"v1\",\"k2\":\"v2\",\"o2\":{\"ok1\":\"ov1\"},\"ak1\":[\"av1\",\"av2\"]}";
var json=JSON.parse(jsonStr,printJson);
2.通过赋值的⽅式定义json对象
形式如下:
var json={"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"ak1":["av1","av2","aqv3"]}
这样就直接获得了⼀个json对象
⼆.Json对象的字符串化
由json对象转化为string字符串 ***JSON.stringify(value[, replacer[, space]])***
1.参数
value 需要进⾏转换的json对象,可使⽤⼀的⽅式获取
replacer 可以传⼊函数也可以传⼊数组
space 格式化输出参数,可以为数字,也可以为其他字符.
2.只传⼊value
var json ={"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"]}
var jsonStr=JSON.stringify(json)
console.log(jsonStr)
//输出如下:
/*
qml: {"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"]}
*/
函数接收json⼦对象的key和value,返回值会对json⼦对象的值进⾏修改;返回 undefined时,会略过对应⼦对象的字符串化.PS 原json对象不会被修改
varvar json ={"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"]}
//返回 undefined 会忽略对应⼦对象的字符串化,PS 'return'等同于 'return undefined'
//返回其他值会在字符串化时替换掉对应⼦对象的值
jsonStr=JSON.stringify(json,function(k,v){
if(k==="k2"){//忽略对 k2⼦对象的字符串化
return undefined
//return //和return undefined等价
}
if(k==="ok1"){//替换初始化的值
return "change value"
}
return v
})
console.log(jsonStr)
jsonStr=JSON.stringify(json)//原 json不会被改变
console.log(jsonStr)
//输出如下:
/*
qml: {"k1":"v1","o1":{"ok1":"change value"},"a1":["av1","av2"]}
qml: {"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"]}
*/
4.space使⽤
本质是在每个对象前换⾏,并根据space填⼊格式化字符;为数字时,格式化字符为space指定数量的" ",其他字符时,则直接填⼊指定字符.
PS填⼊的格式化字符是相对于上⼀级⽽⾔的,相对上⼀级的上⼀级,会插⼊两遍格式化字符
var json ={"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"]}
var jsonStr=JSON.stringify(json,null,1)
console.log("1 space:\n",jsonStr)
jsonStr=JSON.stringify(json,null,4)
console.log("4 space:\n",jsonStr)
jsonStr=JSON.stringify(json,null,'\t')
console.log("\\t:\n",jsonStr)
jsonStr=JSON.stringify(json,null,"format")
console.log("some str:\n",jsonStr)
//输出如下:
/*
qml: 1 space:
{
"k1": "v1",
"k2": "v2",
"o1": {
"ok1": "ov1"
},
"a1": [
"av1",
"av2"
]
}
qml: 4 space:
qml: 4 space:
{
"k1": "v1",
"k2": "v2",
"o1": {
"ok1": "ov1"
},
"a1": [
"av1",
"av2"
]
}
qml: \t:
{
"k1": "v1",
"k2": "v2",
"o1": {
"ok1": "ov1"
},
"a1": [
"av1",
"av2"
]
}
qml: some str:
{
format"k1": "v1",
format"k2": "v2",
format"o1": {
formatformat"ok1": "ov1"
format},
format"a1": [
formatformat"av1",
formatformat"av2"
format]
}
*/
三.Json对象对⼦对象的操作
1.访问⼦对象,获取⼦对象的值
1.1使⽤下标的⽅式,即使⽤"[]"操作符
直接在"[]"内输⼊⼦对象的key值即可,⼀般对象key值为字符串,数组内则为数组索引
var json ={"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"]}
console.log("k2 :",json["k2"]);
console.log("o1 :",json["o1"]);//直接返回对象 {"ok1":"ov1"}
console.log("ok1 :",json["o1"]["ok1"]);
console.log("a1 :",json["a1"]);//直接返回数组 ["av1","av2"]
console.log("av1 :",json["a1"][0]);
//输出如下:
/*
qml: k2 : v2
qml: o1 : [object Object]
qml: ok1 : ov2
qml: a1 : [av1,av2]
qml: av1 : av1
*/
1.2使⽤"."操作符访问
直接在"[]"内输⼊⼦对象的key值即可,⼀般对象key值为字符串,数组内则为数组索引
直接在"[]"内输⼊⼦对象的key值即可,⼀般对象key值为字符串,数组内则为数组索引
var json ={"k1":"v1","k2":"v2","o1":{"ok1":"ov2"},"a1":["av1","av2"]}
console.log("k2 :",json.k2);
console.log("o1 :",json.o1);//直接返回对象 {"ok1":"ov1"}
console.log("ok1 :",json.o1.ok1);
console.log("a1 :",json.a1);//直接返回数组 ["av1","av2"]
console.log("av1 :",json.a1[0]);//数组内元素不能直接以".0"⽅式访问
//输出如下:
/*
qml: k2 : v2
qml: o1 : [object Object]
qml: ok1 : ov2
qml: a1 : [av1,av2]
qml: av1 : av1
*/
2.修改已有⼦对象的值
修改⼦对象的值,和获取的⽅式⼀致,只是把对象作为左值就可
2.1使⽤下标的⽅式,即使⽤"[]"操作符
直接在"[]"内输⼊⼦对象的key值即可,⼀般对象key值为字符串,数组内则为数组索引,然后直接等号赋值即可
var json ={"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"]}
json["k2"]="new v2";
console.log("change k2 :",JSON.stringify(json));
json["o1"]["ok1"]="new ov1";
console.log("change ok1 :",JSON.stringify(json));
json["o1"]="new o1"
console.log("change o1 :",JSON.stringify(json));
json["a1"][0]="new av1"
console.log("change av1 :",JSON.stringify(json));
json["a1"]="new a1"
console.log("change a1 :",JSON.stringify(json));
//输出如下:
/
*
qml: change k2 : {"k1":"v1","k2":"new v2","o1":{"ok1":"ov1"},"a1":["av1","av2"]}
qml: change ok1 : {"k1":"v1","k2":"new v2","o1":{"ok1":"new ov1"},"a1":["av1","av2"]}
qml: change o1 : {"k1":"v1","k2":"new v2","o1":"new o1","a1":["av1","av2"]}
qml: change av1 : {"k1":"v1","k2":"new v2","o1":"new o1","a1":["new av1","av2"]}
qml: change a1 : {"k1":"v1","k2":"new v2","o1":"new o1","a1":"new a1"}
*/
2.2使⽤"."操作符⽅式
直接在"[]"内输⼊⼦对象的key值即可,⼀般对象key值为字符串,数组内则为数组索引
var json ={"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"]}
json.k2="new v2";
console.log("change k2 :",JSON.stringify(json));
json.o1.ok1="new ov1";
console.log("change ok1 :",JSON.stringify(json));
json.o1="new o1"
console.log("change o1 :",JSON.stringify(json));
json.a1[0]="new av1"//数组内元素不能直接以".0"⽅式访问
console.log("change av1 :",JSON.stringify(json));
json.a1="new a1"
console.log("change a1 :",JSON.stringify(json));
//输出如下:
/*
qml: change k2 : {"k1":"v1","k2":"new v2","o1":{"ok1":"ov1"},"a1":["av1","av2"]}
qml: change ok1 : {"k1":"v1","k2":"new v2","o1":{"ok1":"new ov1"},"a1":["av1","av2"]}
qml: change ok1 : {"k1":"v1","k2":"new v2","o1":{"ok1":"new ov1"},"a1":["av1","av2"]}
qml: change o1 : {"k1":"v1","k2":"new v2","o1":"new o1","a1":["av1","av2"]}
qml: change av1 : {"k1":"v1","k2":"new v2","o1":"new o1","a1":["new av1","av2"]}
qml: change a1 : {"k1":"v1","k2":"new v2","o1":"new o1","a1":"new a1"}
*/
3.新增⼦对象
新增⼦对象的值,和修改的⽅式⼀致,只是赋值的的对象原本不存在
2.1使⽤下标的⽅式,即使⽤"[]"操作符
直接在"[]"内输⼊⼦对象的key值即可,⼀般对象key值为字符串,数组内则为数组索引,然后直接等号赋值即可
var json ={"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"]}
json["k3"]="v3";
console.log("add k3 :",JSON.stringify(json));
json["o2"]={"ok1":"ov1"};
console.log("add o2 :",JSON.stringify(json));
json["a2"]=["av1","av2"]
console.log("add a1 :",JSON.stringify(json));
json["a1"][2]="av3"
console.log("add av3 :",JSON.stringify(json));
//输出如下:
/*
qml: add k3 : {"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"],"k3":"v3"}
qml: add o2 : {"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"],"k3":"v3","o2":{"ok1":"ov1"}}
qml: add a1 : {"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"],"k3":"v3","o2":{"ok1":"ov1"},"a2":["av1","av2"]} qml: add av3 : {"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2","av3"],"k3":"v3","o2":{"ok1":"ov1"},"a2":["av1","av2"]} */
2.2使⽤"."操作符⽅式
直接在"[]"内输⼊⼦对象的key值即可,⼀般对象key值为字符串,数组内则为数组索引
var json ={"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"]}
json.k3="v3";
console.log("add k3 :",JSON.stringify(json));
json.o2={"ok1":"ov1"};
console.log("add o2 :",JSON.stringify(json));
json.a2=["av1","av2"]
console.log("add a1 :",JSON.stringify(json));
json.a1[2]="av3"
json转换对象console.log("add av3 :",JSON.stringify(json));
//输出如下:
/*
qml: add k3 : {"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"],"k3":"v3"}
qml: add o2 : {"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"],"k3":"v3","o2":{"ok1":"ov1"}}
qml: add a1 : {"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"],"k3":"v3","o2":{"ok1":"ov1"},"a2":["av1","av2"]} qml: add av3 : {"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2","av3"],"k3":"v3","o2":{"ok1":"ov1"},"a2":["av1","av2"]} */
4.删除已有⼦对象
删除⼦对象的值,有两种⽅式
3.1给要删除的对象赋值为 undefined ,操作⽅式和修改已有⼦对象的值⽅法⼀致.详见三.2
//删除⽰例,只举⼀个,操作⽅式和修改已有⼦对象的值⽅法⼀致.详见三.2
var json ={"k1":"v1","k2":"v2","o1":{"ok1":"ov1"},"a1":["av1","av2"]}
json.k1=undefined
console.log(JSON.stringify(json))
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论