How to get a data set from a row?

please help bring an array of data. spring is here :

{"news": [{"img": "http://static2.smi2.net/img/160x120/2212764.jpeg", "title": "   2 - 3 !  ", "url": "http://news.smi2.ru/newdata/news?ad=681120&bl=81060&ct=adpreview&st=16&in=lJUrBQDHL4CgZAoA", "id": "681120"}]}

I do the following:

var massive = JSON.parse('http://news.smi2.ru/data/js/81060.js');

console.log(massive.news.id);
console.log(massive.news.img);
console.log(massive.news.title);
console.log(massive.news.url);

As a result, you receive the following error message:

Uncaught SyntaxError: Unexpected h token

use only native js

+4
source share
2 answers

You cannot parse Json from url. If you need data from a URI, send an ajax request for this: // A simple javascript example.

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
     var massive = JSON.parse(xmlhttp.responseText);

console.log(massive.news[0].id);
console.log(massive.news[0].img);
console.log(massive.news[0].title);
console.log(massive.news[0].url);
    }
}
xmlhttp.open("GET","http://news.smi2.ru/data/js/81060.js",true);
xmlhttp.send();
+3
source

You are trying to parse the url, not json. You can pass the answer you get and analyze it with JSON.parse:

var massive = JSON.parse('{"news": [{"img": "http://static1.smi2.net/img/160x120/2269036.jpeg", "title": "      ", "url": "http://news.smi2.ru/newdata/news?ad=696406&bl=81060&ct=adpreview&st=16&in=uU6IBQAiL4BWoAoA", "id": "696406"}]}');


console.log(massive.news[0].id);//outputs 696406
Run codeHide result

, URL-, , , ajax:

 $.ajax({
  url: "http://news.smi2.ru/data/js/81060.js",
  dataType : "json"
}).done(function(res) {
    console.log(res.news[0].id);//outputs 696463
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Hide result
+6

All Articles