K
K
kripok952015-12-13 03:49:02
JavaScript
kripok95, 2015-12-13 03:49:02

How to parse JSON from server?

Server sends
[{"id":"1","creation_date":"1449957335","delivery_address":"Kyiv, Amosova, 10","Seller_id":"1","Courier_id":"1","package_cost ":"1000","delivery_cost":"100"}]
I can't help but parse and output to HTML markup, please help.
What should I do?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
AnjeyTsibylskij, 2015-12-13
@AnjeyTsibylskij

Everything is pretty simple

var string_from_server = '[{"id":"1","creation_date":"1449957335","delivery_address":"Kyiv, Amosova, 10","Seller_id":"1","Courier_id":"1","package_cost":"1000","delivery_cost":"100"}]';

var string_to_object = JSON.parse(string_from_server);

console.log(string_to_object);

D
Dark_Scorpion, 2015-12-13
@Dark_Scorpion

Give an example of code that does not work for you. It's just that at first glance everything is simple: there is a json string, from which an object is easily made, with which there is already a standard interaction.

A
Anton Shcherbakov, 2015-12-13
@WestTrade

function rsp(response) {
    var arr = []; // создаем массив 
    for (var i = 0; i < response.length; ++i) {
      // считаем количество записей, выполняем цикл
      var html = '' + 
        '<div>' + 
          response[i].id + // например строим блоки только с id
        '</div>' + 
      ''; // строим элементы DOM

      arr.push(html); // пушим в массив 
    };
    return arr.join(''); // возвращаем данные
  }


  function pst(response) {
    var paste = function rsp(response); // запрашиваем ф-цию построения DOM элементов
    $('html').append(paste);
  }


  $.ajax({
    ...
    success: function(response) {
      // успешный ответ, сервер вернул какие-то данные
      // проверяем на ошибки
      if (typeof(response) === 'undefined' || response === null) {
        console.log('пустой ответ');
      } else {
        function pst(response); // вызываем функцию добавления данных в контент
      }
    },
    error: function() {
    }
  });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question