How to display JSON object values?

Here is what I got so far. Please read the comment in the code. It contains my questions.

var customer;   //global variable
function getCustomerOption(ddId){
      $.getJSON("http://localhost:8080/WebApps/DDListJASON?dd="+ddId, function(opts) {
           $('>option', dd).remove(); // Remove all the previous option of the drop down
           if(opts){  
                customer = jQuery.parseJSON(opts); //Attempt to parse the JSON Object.
           }
      });
}

function getFacilityOption(){
      //How do I display the value of "customer" here. If I use alert(customer), I got null
}

This is what should look like my json-object: {"3":"Stanley Furniture","2":"Shaw","1":"First Quality"}. I ultimately want that if I pass the key 3, I want to get Stanley Furnitureback, and if I go through Stanley Furniture, I get 3back. Because it 3is customerId, and Stanley Furnitureis the name customerName in my database.

+3
source share
3 answers

JSON ( URL-), jQuery $.getJSON(), JSON. jQuery.parseJSON(). . getFacilityOption() $.getJSON() function(opts) ( ).

JSON

{"3":"Stanley Furniture","2":"Shaw","1":"First Quality"}

... " "

var json = {"3":"Stanley Furniture","2":"Shaw","1":"First Quality"};
alert(json['3']);
// or
var key = '3';
alert(json[key]);

JSON, . $.getJSON, .

+6

getJSON XHR. , , , getJSON - , jQuery , . , customer , .

parseJSON JavaScript:

var parsed = jQuery.parseJSON('{"foo":"bar"}');
alert(parsed.foo); // => alerts "bar"

.. , BalusC, -, jQuery , JS .

+2
var customer;   //global variable
function getCustomerOption(ddId){
      $.getJSON("http://localhost:8080/WebApps/DDListJASON?dd="+ddId, function(opts) {
           $('>option', dd).remove(); // Remove all the previous option of the drop down
           if(opts){  
                customer = opts; //Attempt to parse the JSON Object.
           }
      });
}

function getFacilityOption(){
  for(key in costumer)
  {
    alert(key + ':' + costumer[key]);   
  }
}
+2
source

Source: https://habr.com/ru/post/1735285/


All Articles