I get a JSON string by reference: https://app.heroku.com/vac/2 (link for example)

How can I parse JSON with javascript or jquery?

  • one
    JSON.parse(data) ? - BOPOH

2 answers 2

JSON.parse can be used to parse JSON strings .

 JSON.parse('{}'); // {} JSON.parse('true'); // true JSON.parse('"foo"'); // "foo" JSON.parse('[1, 5, "false"]'); // [1, 5, "false"] JSON.parse('null'); // null 

When using the jQuery.getJSON function, jQuery.getJSON do not need to directly call JSON.parse , since it is already called internally.

Since jQuery.getJSON just a wrapper over a regular query, the result can also be obtained in the same way. For example, in the success handler

 $.getJSON( "url", function( data ) { //data - javascript объект, результат обработки полученного json с помощью JSON.parse }); 

plunkr example

  • Can't get the field title var a = jQuery.getJSON('https://app.heroku.com/vac/2'); var b = JSON.parse(a); alert( b.title ); var a = jQuery.getJSON('https://app.heroku.com/vac/2'); var b = JSON.parse(a); alert( b.title ); - Vlad
  • @Vlad , of course it does not work, look at the jQuery.getJSON help and examples of how to use it - Grundy
  • Please give an example, I tried a lot of things, it does not work. - Vlad
  • @ Vlad, updated the answer - Grundy
  • @Vlad, see the errors in the browser console - Grundy

Parsit JSON.parse, but to avoid parsing errors, you need to intercept it or fly errors.

 (url) => { let data; try { data = JSON.parse(url); // Парсим } catch(e) { data = null; // Перехватываем ошибку } if (data==null) return false; // Значит ошибка синтаксиса return data; // 100% валидный результат парсинга }