1

As you can read this JSON using jQuery or JavaScript. I never use JSON.

{
    "ES": {
        "130": {
            "code": "A Coruсa",
            "name": "A Coruña"
        },
        "131": {
            "code": "Alava",
            "name": "Alava"
        },
        "132": {
            "code": "Albacete",
            "name": "Albacete"
        }

To a select:

<select id="provincias">
    <option value="130">A Coruña</option>
    <option value="131">Alava</option>
    <option value="132">Albacete</option>
</select>
4
  • what is the problem? Commented Oct 21, 2015 at 9:00
  • 1
    Your question doesn't make sense, can you try to explain what the issue is. Commented Oct 21, 2015 at 9:01
  • 1
    Mind readers, Its your turn to answer this question.. Commented Oct 21, 2015 at 9:01
  • The best idea is start to collect info about similar problems like, stackoverflow.com/questions/9071525/… Commented Oct 21, 2015 at 9:07

2 Answers 2

1

You do something like this using jQuery.map()

var data = {
  "ES": {
    "130": {
      "code": "A Coruсa",
      "name": "A Coruña"
    },
    "131": {
      "code": "Alava",
      "name": "Alava"
    },
    "132": {
      "code": "Albacete",
      "name": "Albacete"
    }
  }
};

// generate dropddown 
$('<select/>', {
  // setting id attribute
  id: 'provincias',
  // setting html content, iterating over JSON to add options
  html: $.map(data.ES,function(v,i) {
    // creating otions
    return $('<option/>', {
      // setting value as index in json
      value: i,
      // setting text content 
      text: v.name
    })
  })
  //appending the generated dropdown to the body
}).appendTo('body');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

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

Comments

0

You can make use of recursion:

var data = {
  "ES": {
    "130": {
      "code": "A Coruсa",
      "name": "A Coruña"
    },
    "131": {
      "code": "Alava",
      "name": "Alava"
    },
    "132": {
      "code": "Albacete",
      "name": "Albacete"
    }
  }
};

function setOptions(data) { // get data in the args
  var option = ''; // create a var to hold the options to be created.
  for (key in data) { // use for in loop to get the key and values
    if (data[key].constructor === Object) { // check if data is an object
      option += '<option value="' + key + '">' + data[key].name + '</option>'; // create option
      setOptions(data[key]); // pass the object again in the same function to get the "name" value.
    }
  }
  document.getElementById('provincias').innerHTML = option; // finally push in the dom.
}
setOptions(data.ES); // pass the data here, we're targeting ES5
<select id="provincias">
</select>

Comments

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.