如何将两个变量中的 json 响应分开?

发布于 2024-11-06 01:42:24 字数 545 浏览 1 评论 0原文

现在 json 响应看起来

["AG","ANTIGUA AND BARBUDA","AU","AUSTRALIA","BR","BRAZIL","CA","CANADA"] 

我可以使它

["AG,ANTIGUA AND BARBUDA","AU,AUSTRALIA","BR,BRAZIL","CA,CANADA"]

更容易使用或任何其他样式。

我需要的是将其分成两个变量,一个用于国家代码,另一个用于国家名称

   ...success: function( json ) {
    $.each(json, function(i, value) {
          $('#country').append($('<option>').text(value).attr('value', value));
    };  
      });

,现在相同的变量进入选项的值和名称,我需要将国家代码放入选项中,将国家名称放入变量中。

Right now json response looks like

["AG","ANTIGUA AND BARBUDA","AU","AUSTRALIA","BR","BRAZIL","CA","CANADA"] 

i can make it

["AG,ANTIGUA AND BARBUDA","AU,AUSTRALIA","BR,BRAZIL","CA,CANADA"]

if thas easier to work with or any other style.

what i need is to separate it into two variables one for county code other for country name

   ...success: function( json ) {
    $.each(json, function(i, value) {
          $('#country').append($('<option>').text(value).attr('value', value));
    };  
      });

now same variable goes in value and name of option i need to put country code in option and country name in variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

后知后觉 2024-11-13 01:42:24

为什么不首先将它们作为 JSON 对象返回?类似的事情

[{"ID":"AG", "Text":"ANTIGUA AND BARBUDA"},{"ID":"AU", "Text":"Australia"}]

然后你可以做类似的事情

$.each( result, function( value ){
    $('#country').append(
        $("<option>").text(value.Text).attr("value", value.Text)
    );
});

编辑:
上面的代码假设您已经将从服务器发送的数据转换为 JavaScript 数组。有两种方法可以解决此

  1. 问题:您可以使用 getJSON< /a> 方法,jQuery 将为您解析返回值
  2. 您可以使用 自己解析数据parseJSON

这两种方法都会采用格式良好的 JSON 字符串,并返回一个可供使用的 JavaScript 对象

Why not return them as JSON objects in the first place? Something like

[{"ID":"AG", "Text":"ANTIGUA AND BARBUDA"},{"ID":"AU", "Text":"Australia"}]

Then you could do something like

$.each( result, function( value ){
    $('#country').append(
        $("<option>").text(value.Text).attr("value", value.Text)
    );
});

EDIT:
The above code assumes you've already converted the data sent from the server into a JavaScript array. There are two ways to approach that:

  1. You can request the data using the getJSON method and jQuery will parse the return value for you
  2. You can parse the data yourself using the parseJSON

Either method will take a well formated JSON string and give you back a JavaScript object to work with

紫罗兰の梦幻 2024-11-13 01:42:24

如果您选择第二个选项,您可以拆分字符串因此

var countries = jQuery.parseJSON(json);
$.each(countries, function(i, value) {
    var country = value.split(",");
    $('#country').append($('<option>').text(country[1]).attr('value', country[0]));

};

编辑:您需要首先解析 JSON 字符串,以便可以使用数组对象并循环遍历它。请参阅上面我更新的代码。

If you go with your 2nd option you can split the string as such

var countries = jQuery.parseJSON(json);
$.each(countries, function(i, value) {
    var country = value.split(",");
    $('#country').append($('<option>').text(country[1]).attr('value', country[0]));

};

EDIT: You need to parse the JSON string first, so you can use the Array Object and loop through it. See my updated code above.

反差帅 2024-11-13 01:42:24

如果你能做到:

[["AG","ANTIGUA AND BARBUDA"],["AU","AUSTRALIA"],["BR","BRAZIL"],["CA","CANADA"]] 

那么你可以使用这个:

success: function( json ) {
  $.each(json, function(i, value) {
    $('#country').append($('<option>').text(value[1]).attr('value', value[0]));
  };  
});

If you can make it:

[["AG","ANTIGUA AND BARBUDA"],["AU","AUSTRALIA"],["BR","BRAZIL"],["CA","CANADA"]] 

Then you could use this:

success: function( json ) {
  $.each(json, function(i, value) {
    $('#country').append($('<option>').text(value[1]).attr('value', value[0]));
  };  
});
拥抱没勇气 2024-11-13 01:42:24

如果您想采用以下方法:

var array = ["AG","ANTIGUA AND BARBUDA","AU","AUSTRALIA","BR","BRAZIL","CA","CANADA"];

并使其成为键/值对对象的数组,您可以执行以下操作:

result = [];

for (var i = 0; i < array.length; i = i + 2) {
  result.push({ array[i] : array[i + 1]});
}

您最终会在 result 中得到与此等效的结果:

result = [
  { "AG" : "ANTIGUA AND BARBUDA" },
  { "AU" : "AUSTRALIA" },
  { "BR" : "BRAZIL" },
  { "CA" : "CANADA" }
];

更新:如果全部您关心的是将这些添加到选择中,您可以这样做:

var array = ["AG","ANTIGUA AND BARBUDA","AU","AUSTRALIA","BR","BRAZIL","CA","CANADA"];

for (var i = 0; i < array.length; i = i + 2) {
  // Using this object initializer to build the options has a caching benefit.
  $('#country').append($('<option>', {
    value: array[i],
    text: array[i + 1]
  }));
}

对于它的价值,在这种情况下可能可以忽略不计,但这种方法相对较慢。如果您发现性能问题,请考虑在添加选项时使用 .detach() 方法将 #country 从 DOM 中拉出,然后将其附加回结尾。

If you want to take this:

var array = ["AG","ANTIGUA AND BARBUDA","AU","AUSTRALIA","BR","BRAZIL","CA","CANADA"];

And make it an array of key/value pair objects, you could do something like this:

result = [];

for (var i = 0; i < array.length; i = i + 2) {
  result.push({ array[i] : array[i + 1]});
}

You'll end up with the equivalent of this in result:

result = [
  { "AG" : "ANTIGUA AND BARBUDA" },
  { "AU" : "AUSTRALIA" },
  { "BR" : "BRAZIL" },
  { "CA" : "CANADA" }
];

Update: If all you care about is adding those to the select, you can do it this way:

var array = ["AG","ANTIGUA AND BARBUDA","AU","AUSTRALIA","BR","BRAZIL","CA","CANADA"];

for (var i = 0; i < array.length; i = i + 2) {
  // Using this object initializer to build the options has a caching benefit.
  $('#country').append($('<option>', {
    value: array[i],
    text: array[i + 1]
  }));
}

For what it's worth, it may be negligible in this case, but this approach is relatively slow. If you notice performance issues, look into using the .detach() method to pull #country out of the DOM while the options are being added and then attaching it back at the end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文