Cleaner
Cleaner

Reputation: 11

How to separate json response in two variables?

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.

Upvotes: 0

Views: 3580

Answers (4)

Roman
Roman

Reputation: 20246

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

Upvotes: 4

Dave Ward
Dave Ward

Reputation: 60580

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.

Upvotes: 0

Marko
Marko

Reputation: 72222

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.

Upvotes: 1

Matthew Scharley
Matthew Scharley

Reputation: 132254

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]));
  };  
});

Upvotes: 0

Related Questions