如何将动态字符串数组从javascript传递到JSON对象

时间:2017-11-28 13:43:17

标签: javascript json

function createNew(mouseEventData) {
 var measurementData = {
            visible: true,
            active: true,
            toolType: 'target',
            description: output
        };

        return measurementData;
}

 function onImageRendered(e, eventData) {
    var textline = ["Test"];
    var obj = { "description":textline};
    alert(obj)//[object Object]
    var output = JSON.stringify(obj);
    alert("output"+output); //output{"description":["Test"]}
}

在上面的代码片段中,我想将textline字符串数组传递给measurementData的description字段。 请帮我介绍如何将动态字符串数组传递给JSON对象 (文本行是动态数组)

1 个答案:

答案 0 :(得分:0)

您可以使用全局变量:

var measurementData;

function createNew(mouseEventData) {
    return measurementData;
}

function onImageRendered(e, eventData) {
    var textline = ["Test"];
    measurementData = {
        visible: true,
        active: true,
        toolType: 'target',
        description: textline
    };
}