F
F
fr1zzer2017-03-06 19:29:34
JavaScript
fr1zzer, 2017-03-06 19:29:34

How to output json?

There are json files with data of the form

{
  "page": "3",
  "total": "26",
  "records": "75247",
  "rows": [
    {
      "id": "af9b185bdc84499fab5258bdb698a305",
      "tenderID": "UA-2017-02-17-002412-c",
      "title": "Конструкційні вироби",
      "status": "active.auction",
      "dateModified": "2017-03-06T09:33:45.641137+02:00"
    }
  ]
}


I'm trying to display tenderID
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>JSON</title>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<script type="application/javascript">
$(function() {
  var url = "http://localhost/123/1.json";

  $.getJSON(url, function(data) {
    displayResults(data.rows);
  });

  function displayResults(jsonResults) {
    for (i = 0; i < jsonResults.length; i++) {
      pageHtmlString += ('' + jsonResults[i].tenderID + '');
    }
  }
});
</script>

</body>
</html>


What is wrong in the code?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
abberati, 2017-03-06
@fr1zzer

function displayResults(jsonResults) {
  const body = document.querySelector('body');
  const resultString = jsonResults.reduce((string, row) => string + `${row.tenderID} `, '');
  body.innerText = resultString;
}

A
Alexey Ukolov, 2017-03-06
@alexey-m-ukolov

A variable pageHtmlStringis taken from nowhere and goes nowhere.

A
Anton Anton, 2017-03-07
@Fragster

For the simplest cases I liked json2html.com

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question