在JSON拆分的Javascript值 [英] Split Javascript values in JSON

查看:108
本文介绍了在JSON拆分的Javascript值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个数组分割成一个JSON数组应以下模式。

  {{URL:URL,北:真正的面:真},{URL:URL,北:假,方:真}}
 

我得到这个code中的URL参数。正如你所看到的这里,这code显示器3的复选框,您可以选择,如果图片是北,上侧面或者,如果你想选择它。

 如果(xmlHttp.readyState == 4 || xmlHttp.readyState ==完成){
    xmlDoc中= xmlHttp.responseXML;
    pictureTemp = [的document.getElementById(imgfilename)];

    $(登录形式)追加。('<按钮的onclick =sendAuswahl()>发送和LT; /按钮>< BR>');
    对于(VAR I = 0; I< xmlDoc.getElementsByTagName(imgfilename)的长度;我++){
      pictureTemp [I] = xmlDoc.getElementsByTagName(imgfilename)[我] .childNodes [0] .nodeValue;
      $('登录形式)追加('<输入类型=复选框名称=北的价值=北><输入类型=复选框名称=方向值=方>&其中;输入类型=复选框名称=网址值='+ pictureTemp [I] +'>&所述; IMG SRC =+ pictureTemp [I] +'宽度=50%>&所述; / BR>');
    };
}
 

要获得所有选中复选框,我用这个code:

  VAR arrayUrl = $(输入[名称='URL']:检查)图(函数(){。
      返回THIS.VALUE;
})。得到()

VAR arrayNorth = $(输入[名称='北']:选中)。图(函数(){
      返回True;
})。得到()

VAR arrayOrientation = $(输入[名称='方向']:检查)图(函数(){。
      返回True;
})。得到()
 

要选择转换为JavaScript对象,并获得我上述的模式,我用这样的:

  VAR照片= {
      URL:arrayUrl,
      北:arrayNorth,
      侧:arrayOrientation
};
 

但是,当我提醒所选图像的价值我得到这样的:

  {URL:HTTP://www.example.com,北:真正的面:真}
 

当我选择2图像我得到这样的:

  {URL:HTTP://www.example.com,http://www.example2.com,北:真正的面:假}
 

取而代之的是

  {{URL:HTTP://www.example.com,北:真正的面:假},{URL:HTTP:// WWW .example2.com,北:假,方:真}}
 

所以我的问题是现在?我怎么能娴熟而我上面描述的模式值

解决方案

  VAR画面= [];
$(arrayUrl)。每个(函数(指数){
    picture.push({
        URL:arrayUrl [指数]
        北:arrayNorth [指数]
        方:arrayOrientation [指数]
    });
});
 

I need to split an array into an JSON array which should be following pattern.

{{"url":url, "north":True "side":True}, {"url":url, "north":False, "side":True}}

I get the url parameter with this code. As you can see here, this code displays 3 checkboxes where you can select if the picture is north, on the side or if you want to select it.

if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
    xmlDoc = xmlHttp.responseXML;
    pictureTemp = [document.getElementById("imgfilename")];

    $('.login-form').append('<button onclick="sendAuswahl()">Send</button><br>');
    for (var i = 0; i < xmlDoc.getElementsByTagName("imgfilename").length; i++) {
      pictureTemp[i] = xmlDoc.getElementsByTagName("imgfilename")[i].childNodes[0].nodeValue;
      $('.login-form').append('<input type="checkbox" name="north" value="North"><input type="checkbox" name="orientation" value="Side"><input type="checkbox" name="url" value='+ pictureTemp[i]+'><img src='+ pictureTemp[i]+' width="50%"></br>');
    };
}

To get all ticked checkboxes, I use this code:

var arrayUrl = $("input[name='url']:checked").map(function(){
      return this.value;
}).get()

var arrayNorth = $("input[name='north']:checked").map(function(){
      return "True";
}).get()

var arrayOrientation = $("input[name='orientation']:checked").map(function(){
      return "True";
}).get()

To convert the selection to a JavaScript object and to get the pattern which I described above, I use this:

var picture = {
      "url" : arrayUrl,
      "North" : arrayNorth,
      "Side" : arrayOrientation
};

But when I alert the value of a selected image I get this:

{"url":http://www.example.com, "north":True "side":True}

And when I select 2 images I get this:

{"url":http://www.example.com, http://www.example2.com, "north":True "side":False}

Instead of this:

{{"url":http://www.example.com, "north":True "side":False}, {"url":http://www.example2.com, "north":False, "side":True}}

So my question is now: How can I adept the values in the pattern which I've described above?

解决方案

var picture = [];
$(arrayUrl).each(function(index) {
    picture.push({
        "url": arrayUrl[index],
        "North": arrayNorth[index],
        "Side": arrayOrientation[index]
    });
});

这篇关于在JSON拆分的Javascript值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆