0

I have a json data in following format

 {"Jack":["[email protected]","US"],"Rob":["[email protected]","UK"]}

i have a select tag, if the value selected from a select box equal to "Jack" then it should print the data "[email protected]","US"

select box on change function is

       function printDataOf(val){

        now if selectd value is Jack
        alert()   // should alert [email protected]
        alert()   // should alert US

how do i do it?

4
  • 2
    alert(data[val][0]) Commented Sep 3, 2013 at 10:29
  • @thg435 That is not working :( Commented Sep 3, 2013 at 10:30
  • replace "data" with whatever your JSON array is called. Commented Sep 3, 2013 at 10:32
  • @thg435 I was making a silly mistake, while doing I have written it as alert(json_obj[Jack][0]); instead of alert(json_obj["Jack"][0]);. You're right. Commented Sep 3, 2013 at 10:40

8 Answers 8

1

This should do it;

printDataOf('Jack');

function printDataOf(val) {

    var data = '{"Jack":["[email protected]","US"],"Rob":["[email protected]","UK"]}';

    var match = JSON.parse(data)[val];
    var email = match[0];
    var country = match[1];

    alert(email);
    alert(country);

}
Sign up to request clarification or add additional context in comments.

Comments

1

The onchange function will not be able to receive the actual value. You can pass the select object (as 'this') though.

<select onchange="printDataOf(this)">
    <option/>
    <option value="Jack">Jack</option>
    <option value="Rob">Rob</option>
</select>

Now in the handler you can get the actual selected value and display the desired data.

var json_obj =  {"Jack":["[email protected]","US"],"Rob":["[email protected]","UK"]};

function printDataOf(val){
  var Mval = $(val).val();
  alert(json_obj[Mval][0]);
  alert(json_obj[Mval][1]);
}

It would be wise to check for existence of Mval in the json object. If it is not there you will receive an error. I didn't do it here to keep it simple.

Here is a working example.

Comments

0

This should work.

    function check( json_obj ) 
{
    return json_obj[0] == 'Jack'
}

if you're using jquery, also try this:

json_obj = jQuery.parseJSON( json_obj.responseText );

To examine what you really got, use firebug's debugger

Comments

0

Try this, change json with your actual object:

function printDataOf(val)
{
   for (name in json)
   {
      if (name == val)
      {
          alert(json.name[0]);
          alert(json.name[1]);
      }
   }
}

Comments

0
for (key in jsonobject) {
   alert(jsonobject[val][0];
   alert(jsonobject[val][1];
   alert(jsonobject[val][2];
}

Comments

0

This should get you what you needed,

var result = {"Jack":["[email protected]","US"],"Rob":["[email protected]","UK"]};

function printDataOf(val){
   var data = JSON.parse(result);

   var selected = data[val];
   var selected_arr = selected.split(',');

   //now if selectd value is Jack
   alert(selected_arr[0])   // should alert [email protected]
   alert(selected_arr[1])   // should alert US
}

Comments

0

Loop Json data and then compare and print . Using jquery...

  var data = {"Jack":["[email protected]","US"],"Rob":["[email protected]","UK"]}
var selecteVal="Jack";

$.each(data,function(key,index){
       if (key==selecteVal){
            alert(data[key][0])
             alert(data[key][1])
            }    
       });

See JSFiddle

Comments

0

Try this.

  var data = {"Jack":["[email protected]","US"],"Rob":["[email protected]","UK"]}
  var selecteVal="Jack";

  if(typeof(data[selecteVal])!='undefined'){
     alert(data[selecteVal][0]);//[email protected]
     alert(data[selecteVal][1]);//US
  }

Demo

1 Comment

Yo don't need to run loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.