迭代修改json格式数据

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xtiawxf/article/details/84277299
背景:在运用jquery的treegrid控件时,需要动态展现折叠数据,所以需要每次展开行数据时,将从服务器获取的json数据更新至treegrid的data数据的指定属性值中。


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="textml; charset=utf-8" />
<title>json迭代修改</title>
</head>

<body>
<script type="text/javascript" language="javascript">
var jsonObj = [{"id":"1",name:"a1",children:
[
{"id":"1.1",name:"a1.1"},
{"id":"1.2",name:"a1.2",children:[
{"id":"1.2.1",name:"a1.2.1"}
]
}
]
},
{"id":"2",name:"a2"}
];
/*
*方法名: setJsonValue
*属 性: json,json格式数据对象,源数据
* identifier,标示区分某一行json数据,json格式数据({"name":"","value":""})
* data,需要修改属性以及修改后的值,json格式数据({"name":"","value":""})
*功 能: 根据identifier,确定json行数据,然后修该行指定属性(data["name"])的值(data["value"])
*返回值: 返回修改后的json对象
**/
function setJsonValue(json,identifier,data){
var item;
for(var i=0; i<json.length;i++){
item = json[i];
if(Object.prototype.toString.apply(item[data["name"]])=="[object Array]" || Object.prototype.toString.apply(item[data["name"]]) == "[object Object]") {
item[data["name"]]=setJsonValue(item[data["name"]],identifier,data);
}
if(item[identifier["name"]]==identifier["value"]){
item[data["name"]]=data["value"];
break;
}

}
return json
}
/*
*方法名:parseJson
*属 性:json,json格式数据
*返回值:无
*/
function parseJson(json){
var item;
for(var itemname in json){
item = json[itemname]
if(Object.prototype.toString.apply(item)=="[object Array]" || Object.prototype.toString.apply(item) == "[object Object]") {
parseJson(item);
}else{
alert("属性:"+itemname+",值:"+item);
}
}
}
jsonObj = setJsonValue(jsonObj,{"name":"id","value":"2"},{"name":"children","value":[{"id":"2.1",name:"a2.1"}]});
parseJson(jsonObj)
</script>
</body>
</html>

展开阅读全文

修改json格式

07-04

使用js对json进行操作,"AreaName":"江苏省","AreaCode":"320000","AreaName":"南京市","AreaCode":"320100","AreaName":"市辖区","AreaCode":"320101","AreaName":"玄武区","AreaCode":"320102","AreaName":"白下区","AreaCode":"320103","AreaName":"秦淮区","AreaCode":"320104","AreaName":"建邺区","AreaCode":"320105","AreaName":"鼓楼区","AreaCode":"320106","AreaName":"下关区","AreaCode":"320107","AreaName":"浦口区","AreaCode":"320111","AreaName":"栖霞区","AreaCode":"320113","AreaName":"雨花台区","AreaCode":"320114","AreaName":"江宁区","AreaCode":"320115","AreaName":"六合区","AreaCode":"320116","AreaName":"溧水县","AreaCode":"320124","AreaName":"高淳县","AreaCode":"320125"这种样式的数据转换成rnrn"AreaName":"江苏省","AreaCode":"320000","cityList":["AreaName":"南京市","AreaCode":"320100","areaList":["AreaName":"市辖区","AreaCode":"320101","AreaName":"玄武区","AreaCode":"320102","AreaName":"白下区","AreaCode":"320103","AreaName":"秦淮区","AreaCode":"320104","AreaName":"建邺区","AreaCode":"320105","AreaName":"鼓楼区","AreaCode":"320106","AreaName":"下关区","AreaCode":"320107","AreaName":"浦口区","AreaCode":"320111","AreaName":"栖霞区","AreaCode":"320113","AreaName":"雨花台区","AreaCode":"320114","AreaName":"江宁区","AreaCode":"320115","AreaName":"六合区","AreaCode":"320116","AreaName":"溧水县","AreaCode":"320124","AreaName":"高淳县","AreaCode":"320125"]]rn这种形式的,用js怎么实现??大神求帮助 论坛

JSON格式数据

05-09

要把下面的格式转化成rn[code=javascript]rn[rn rn "city": "杭州", rn "contry": "中国", rn "id": "2bf4cf31d7ef", rn "name": "df inc", rn "province": "浙江"rn , rn rn "city": "富阳", rn "contry": "中国", rn "id": "d57395b8c022", rn "name": "df inc", rn "province": "浙江"rn , rn rn "city": "武汉", rn "contry": "中国", rn "id": "d51ce43bc4f9", rn "name": "df inc", rn "province": "湖北"rn , rn rn "city": "赤壁", rn "contry": "中国", rn "id": "e6032e6bd463", rn "name": "df inc", rn "province": "湖北"rn , rn rn "city": "测试A", rn "contry": "美国", rn "id": "a013f32f2a42", rn "name": "df inc", rn "province": "华盛顿"rn , rn rn "city": "测试一", rn "contry": "美国", rn "id": "5cfe871daf17", rn "name": "df inc", rn "province": "洛杉矶"rn rn]rn[/code]rnrnrn转化成rn[code=javascript]rn[rn rn "contry": "美国", rn "provinces": [rn rn "citys": [rn rn "city": "测试A", rn "contry": "美国", rn "id": "aed2960b668d", rn "name": "df inc", rn "province": "华盛顿"rn rn ], rn "province": "华盛顿"rn , rn rn "citys": [rn rn "city": "测试一", rn "contry": "美国", rn "id": "deb60d6b14bb", rn "name": "df inc", rn "province": "洛杉矶"rn rn ], rn "province": "洛杉矶"rn rn ]rn , rn rn "contry": "中国", rn "provinces": [rn rn "citys": [rn rn "city": "富阳", rn "contry": "中国", rn "id": "e3242f01cb52", rn "name": "df inc", rn "province": "浙江"rn , rn rn "city": "杭州", rn "contry": "中国", rn "id": "3a576952c07e", rn "name": "df inc", rn "province": "浙江"rn rn ], rn "province": "浙江"rn , rn rn "citys": [rn rn "city": "武汉", rn "contry": "中国", rn "id": "58455d2acac9", rn "name": "df inc", rn "province": "湖北"rn , rn rn "city": "赤壁", rn "contry": "中国", rn "id": "5199b22b7ca0", rn "name": "df inc", rn "province": "湖北"rn rn ], rn "province": "湖北"rn rn ]rn rn]rn[/code]rnrnrn想知道一种少循环的方法,我现在转化的方法循环次数比较多。 论坛

没有更多推荐了,返回首页