Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中附加JSON对象_Javascript_Jquery_Json - Fatal编程技术网

在Javascript中附加JSON对象

在Javascript中附加JSON对象,javascript,jquery,json,Javascript,Jquery,Json,我将动态创建JSON对象,如下所示: [{"fill":"none","stroke":"#000000","path":"M186.5,25L187.5,25L187.5,26L188.5,27L189.5,28L189.5,29L190.5,29","stroke-opacity":1,"stroke-width":5,"stroke-linecap":"round","stroke-linejoin":"round","transform":[],"type":"path"}] [{"f

我将动态创建JSON对象,如下所示:

[{"fill":"none","stroke":"#000000","path":"M186.5,25L187.5,25L187.5,26L188.5,27L189.5,28L189.5,29L190.5,29","stroke-opacity":1,"stroke-width":5,"stroke-linecap":"round","stroke-linejoin":"round","transform":[],"type":"path"}]

[{"fill":"none","stroke":"#000000","path":"M73.5,42L73.5,42L75.5,43L82.5,46L101.5,55L119.5,65L126.5,69L128.5,71L129.5,71","stroke-opacity":1,"stroke-width":5,"stroke-linecap":"round","stroke-linejoin":"round","transform":[],"type":"path"}]

.......
我想将生成的所有这些对象附加到单个Javascript对象中,如下所示:

[{"fill":"none","stroke":"#000000","path":"M186.5,25L187.5,25L187.5,26L188.5,27L189.5,28L189.5,29L190.5,29","stroke-opacity":1,"stroke-width":5,"stroke-linecap":"round","stroke-linejoin":"round","transform":[],"type":"path"},
{"fill":"none","stroke":"#000000","path":"M73.5,42L73.5,42L75.5,43L82.5,46L101.5,55L119.5,65L126.5,69L128.5,71L129.5,71","stroke-opacity":1,"stroke-width":5,"stroke-linecap":"round","stroke-linejoin":"round","transform":[],"type":"path"}]
这样,创建的每个对象都应该附加到此JSON字符串。 我能够连接两个JSON对象,并将其放在不同的Javascript变量中,如下所示:

var obj1 = '[{"fill":"none","stroke":"#000000","path":"M186.5,25L187.5,25L187.5,26L188.5,27L189.5,28L189.5,29L190.5,29","stroke-opacity":1,"stroke-width":5,"stroke-linecap":"round","stroke-linejoin":"round","transform":[],"type":"path"}]';

var obj2 = '[{"fill":"none","stroke":"#000000","path":"M186.5,25L187.5,25L187.5,26L188.5,27L189.5,28L189.5,29L190.5,29","stroke-opacity":1,"stroke-width":5,"stroke-linecap":"round","stroke-linejoin":"round","transform":[],"type":"path"}]';

var mergedJS = JSON.parse(obj1).concat(JSON.parse(obj2));

  mergedJSON =JSON.stringify(mergedJS);
但是,我希望所有新生成的JSON objs都在同一个变量中。
有人能告诉我怎么做吗?

在将对象添加到主阵列之前,您需要将对象从各自的阵列中拉出:

var newJSArray = [];
var mergedJS = JSON.parse(obj1);
newJSArray.push(mergedJS[0]);
mergedJS = JSON.parse(obj2);
newJSArray.push(mergedJS[0]);

显然,对于n个对象,您将循环该对象,而不是像我上面所做的那样。

这些对象是如何生成的?不能直接将它们推送到数组中吗?我不能直接将它们推送到数组中,因为它们是在函数调用中单独生成的。如果可能的话,我如何将它们直接推送到数组中呢?但是这个函数会被调用,它会返回对象吗?这些对象生活在哪里?使用
var mergedJS=JSON.parse(obj1).concat(JSON.parse(obj2))是,这些对象被创建为$(“#data2”).val(sketchpad.json());和被分配到一个隐藏的div,ID=“data2”我希望此时将创建的每个对象添加到数组中。这段代码将完成相同的任务。只需获取每个对象数组的第0个元素,因为您的对象是在封闭数组中发送的。