C
C
Cpa2018-02-15 07:59:29
JavaScript
Cpa, 2018-02-15 07:59:29

How to parse response to variables?

Hello, how to take out in variables from the Json result in this code

<html>
<body>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script type='text/javascript'>
  

$.getJSON('http://ip-api.com/json?callback=?', 
function(data) {
console.log(JSON.stringify(data, null, 2));
});


</script>

</body>
</html>

Answer
{
"as": "AS41794 Sibirskie Seti Ltd.",
"city": "Barnaul",
"country": "Russia",
"countryCode": "RU",
"isp": "Sibirskie Seti Ltd.",
"lat ": 53.36,
"lon": 83.76,
"org": "Sibirskie Seti Ltd.",
"query": "5.44.170.141",
"region": "ALT",
"regionName": "Altai Krai",
"status ": "success",
"timezone": "Asia/Barnaul",
"zip": "656000"
}

How to make city variable with value city,ip with ip value and so on. Thanks!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly Zharov, 2018-02-15
@Cpa

es2015 offers destructuring for such cases

$.getJSON(
    'http://ip-api.com/json?callback=?', 
    function({city, ip, county, ...others}) {
        /*...*/
    }
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question