请问怎么在对象传值时,传到对象的数据时,只取值

相关代码

  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'
    };
    var demo;
    demo=eventAttributes;
    for (var snap of json.SnapPolygonList.SnapPolygon) {
        var pointList = [];
        for (var point of snap.pointList.point) {
            //    pointList.push([point.x, point.y]);
            pointList.push([parseFloat(point.x), parseFloat(point.y)]);
        }
        if (snap.color.r === '0') {
            eventAttributes['布防区域'].push({
                '区域坐标': pointList,
                '区域编号': parseInt(snap.id)
            });
        }
        if (snap.color.r === '255') {
            eventAttributes['撤防区域'].push({
                '撤防区域': pointList,
                '区域编号': parseInt(snap.id)
            });
        }
    };
    console.log(eventAttributes);
    var event= {
        "事件信息": {
            "事件id": 0,
            "事件名称": "测试55",
            "设备id": 2
        },
        "事件参数": {
            demo,
            "开始检测时间": "00:00:00",
            "是否启用": true,
            "结束检测时间": "23:59:59",
            "规则参数": {
                "检测目标": [
                    1
                ],
                "算法参数": {
                    "算法参数": {
                        "停留超时": 0,
                        "帧超时": 500,
                        "报警阈值": 80,
                        "最低阈值": 50,
                        "连续帧数": 10
                    },
                    "算法类型": 2
                },
                "规则类型": 1
            }
        }
    }

    var paramsJson = JSON.stringify(event);

打印的值

{
    "事件参数":{
        "开始检测时间":"00:00:00",
        "结束检测时间":"23:59:59",
        "是否启用":true,
        "demo":{
            "布防区域":[
                {
                    "区域坐标":[
                        [
                            0.324415,
                            0.496516
                        ],
                        [
                            0.538462,
                            0.486063
                        ],
                        [
                            0.438127,
                            0.80662
                        ],
                        [
                            0.269231,
                            0.759582
                        ]
                    ],
                    "区域编号":1
                }
            ],
            "撤防区域":[
                {
                    "区域编号":2,
                    "撤防区域":[
                        [
                            0.168896,
                            0.728223
                        ],
                        [
                            0.650502,
                            0.780488
                        ],
                        [
                            0.493311,
                            0.945993
                        ],
                        [
                            0.143813,
                            0.970383
                        ],
                        [
                            0.157191,
                            0.949477
                        ]
                    ]
                }
            ]
        },
        "规则参数":{
            "检测目标":[
                1
            ],
            "规则类型":1,
            "算法参数":{
                "算法类型":2,
                "算法参数":{
                    "报警阈值":80,
                    "连续帧数":10,
                    "帧超时":500,
                    "最低阈值":50,
                    "停留超时":0
                }
            }
        }
    },
    "事件信息":{
        "设备id":2,
        "事件名称":"测试55",
        "事件id":0
    }
}

需要的值

{
"事件信息":{
    "事件id":0,
    "事件名称":"测试55",
    "设备id":2
},
"事件参数":{

        "布防区域":[
            {
                "区域坐标":[
                    [
                        0.324415,
                        0.496516
                    ],
                    [
                        0.538462,
                        0.486063
                    ],
                    [
                        0.438127,
                        0.80662
                    ],
                    [
                        0.269231,
                        0.759582
                    ]
                ],
                "区域编号":1
            }
        ],
        "撤防区域":[
            {
                "撤防区域":[
                    [
                        0.168896,
                        0.728223
                    ],
                    [
                        0.650502,
                        0.780488
                    ],
                    [
                        0.493311,
                        0.945993
                    ],
                    [
                        0.143813,
                        0.970383
                    ],
                    [
                        0.157191,
                        0.949477
                    ]
                ],
                "区域编号":2
            }
        ]
    ,
    "开始检测时间":"00:00:00",
    "是否启用":true,
    "结束检测时间":"23:59:59",
    "规则参数":{
        "检测目标":[
            1
        ],
        "算法参数":{
            "算法参数":{
                "停留超时":0,
                "帧超时":500,
                "报警阈值":80,
                "最低阈值":50,
                "连续帧数":10
            },
            "算法类型":2
        },
        "规则类型":1
    }
}
阅读 1.3k
1 个回答

从数据的角度看不出来你打印的值和想要的值有什么实质性的区别,感觉上就是你想按你想要的值顺序打印而已。
对此其实因为你可以预定义打印顺序(不直接打印整个json对象),而是按你顺序分别打印各个子对象即可。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题