请问怎么从json对象从取值,放到demo的对象中
var json ={"SnapPolygonList":{"SnapPolygon":[{"id":"1","polygonType":"1","color":{"r":"0","g":"255","b":"0"},"tips":"#1#","isClosed":"true","pointList":{"point":[{"x":"0.324415","y":"0.496516"},{"x":"0.538462","y":"0.486063"},{"x":"0.438127","y":"0.806620"},{"x":"0.269231","y":"0.759582"}]}},{"id":"2","polygonType":"1","color":{"r":"255","g":"0","b":"0"},"tips":"#2#","isClosed":"true","pointList":{"point":[{"x":"0.168896","y":"0.728223"},{"x":"0.650502","y":"0.780488"},{"x":"0.493311","y":"0.945993"},{"x":"0.143813","y":"0.970383"},{"x":"0.157191","y":"0.949477"}]}}]}};
var eventparameter = { "事件参数": {
"布防区域": [{
"区域坐标": [
[0.2001, 0.2001],
[0.5002, 0.2002],
[0.5003, 0.5003],
[0.2004, 0.5004]
],
"区域编号": 1
}
],
"开始检测时间": "00:00:00",
"撤防区域": [
{
"区域坐标": [
[
0.24861111111111112,
0.27256944444444442
],
[
0.65555555555555556,
0.22395833333333334
],
[
0.56805555555555554,
0.55208333333333337
],
[
0.40138888888888891,
0.68055555555555558
]
],
"区域编号": 1
}
]
}
}
var demo = {"事件参数": {
"布防区域": [
{
"区域坐标": [
point
],
"区域编号": id
}
],
"开始检测时间": "00:00:00",
"撤防区域": [
{
"区域坐标": [
point
],
"区域编号": id
}
]
}
};
回答
var json ={"SnapPolygonList":{"SnapPolygon":[{"id":"1","polygonType":"1","color":{"r":"0","g":"255","b":"0"},"tips":"#1#","isClosed":"true","pointList":{"point":[{"x":"0.324415","y":"0.496516"},{"x":"0.538462","y":"0.486063"},{"x":"0.438127","y":"0.806620"},{"x":"0.269231","y":"0.759582"}]}},{"id":"2","polygonType":"1","color":{"r":"255","g":"0","b":"0"},"tips":"#2#","isClosed":"true","pointList":{"point":[{"x":"0.168896","y":"0.728223"},{"x":"0.650502","y":"0.780488"},{"x":"0.493311","y":"0.945993"},{"x":"0.143813","y":"0.970383"},{"x":"0.157191","y":"0.949477"}]}}]}};var eventAttributes = {
'布防区域': [],
'撤防区域': [],
'开始检测时间': '00:00:00'
};
for (var snap of json.SnapPolygonList.SnapPolygon) {
var pointList = [];
for (var point of snap.pointList.point) {
pointList.push([point.x, point.y]);
}
if (snap.color.r === '0') {
eventAttributes['布防区域'].push({
'区域坐标': pointList,
'区域编号': snap.id
});
}
if (snap.color.r === '255') {
eventAttributes['撤防区域'].push({
'撤防区域': pointList,
'区域编号': snap.id
});
}
};
console.log(eventAttributes);
var bb =[{ "x": "0.008361",
"y": "0.005587"
}, {
"x": "0.991639",
"y": "0.019553"
}, {
"x": "0.991639",
"y": "0.986033"
}, {
"x": "0.006689",
"y": "0.980447"
}];
function helpTransform(data) {
var result = []
var isArray = Object.prototype.toString.call(data) === '[object Array]'
if (!isArray) {
return result
}
for(var i = 0, len = data.length; i < len; i++) {
var temp = data[i]
var arr = [temp.x, temp.y]
result.push(arr)
}
return result
}
var dd =helpTransform(bb);
console.log(dd);
var json ={"SnapPolygonList":{"SnapPolygon":[{"id":"1","polygonType":"1","color":{"r":"0","g":"255","b":"0"},"tips":"#1#","isClosed":"true","pointList":{"point":[{"x":"0.324415","y":"0.496516"},{"x":"0.538462","y":"0.486063"},{"x":"0.438127","y":"0.806620"},{"x":"0.269231","y":"0.759582"}]}},{"id":"2","polygonType":"1","color":{"r":"255","g":"0","b":"0"},"tips":"#2#","isClosed":"true","pointList":{"point":[{"x":"0.168896","y":"0.728223"},{"x":"0.650502","y":"0.780488"},{"x":"0.493311","y":"0.945993"},{"x":"0.143813","y":"0.970383"},{"x":"0.157191","y":"0.949477"}]}}]}};
var eventAttributes = {
'布防区域': [],
'撤防区域': [],
'开始检测时间': '00:00:00'
};
json.SnapPolygonList.SnapPolygon.forEach(function(snap) {
if (snap.color.r === '0') {
eventAttributes['布防区域'].push({
'区域坐标': helpTransform(snap.pointList),
'区域编号': snap.id
});
}
if (snap.color.r === '255') {
eventAttributes['撤防区域'].push({
'撤防区域': helpTransform(snap.pointList),
'区域编号': snap.id
});
}
});
console.log(eventAttributes);
以上是 请问怎么从json对象从取值,放到demo的对象中 的全部内容, 来源链接: utcz.com/a/32507.html