Answer the question
In order to leave comments, you need to log in
How to parse json array into 2 different arrays?
I have a json array and I need to parse it into 2 different arrays. Here's what it looks like:
"United Arab Emirates" : "AED",
"Afghanistan" : "AFN",
"Albania" : "ALL",
"Armenia" : "AMD",
"Netherlands Antilles" : "ANG",
"Angola" : "AOA",
"Argentina" : "ARS",
"Australia" : "AUD",
Answer the question
In order to leave comments, you need to log in
When parsing the given JSON, you will get not an array, but an object. Couples ключ: значение
.
If only keys are needed, there is the Object.keys() method , which will return an array of keys:
var s = '{"United Arab Emirates":"AED","Afghanistan":"AFN","Albania":"ALL","Armenia":"AMD","Netherlands Antilles":"ANG","Angola":"AOA","Argentina":"ARS","Australia":"AUD"}';
var data = JSON.parse(s);
var countries = Object.keys(data);
// United Arab Emirates,Afghanistan,Albania,Armenia,Netherlands Antilles,Angola,Argentina,Australia
var data = JSON.parse(s, function(k,v){
return typeof v === "object" ? Object.keys(v) : v;
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question