Parse json strings:

$.ajax({ dataType: 'json', url: parser + ip, success: function(data){ console.log(data); //var country = $.parseJSON('"country_code"'); //console.log(country); } }); 

and get the following line: {"country_code":"RU"} . How can I get the value of the country_code field from the json string?

    2 answers 2

     $.ajax({ dataType: 'json', url: parser + ip, success: function(data){ var data = JSON.parse(data);// Преобразует строку в объект console.log(data.country_code);// Вытаскиваем значение свойства объекта } }); 
    • Uncaught SyntaxError: Unexpected token o in JSON at position 1 - JamesJGoodwin
    • What is parsing? Answer type json , jQuery itself will try to parse the answer. - user207618
    • Uncaught SyntaxError: Unexpected token o in JSON at position 1 If such an error means a different type of data comes to data: {"country_code": "RU"} - user190134

    It turned out that the use of parsing functions is not necessary.

     var parsed = data.country_code;