D
D
doublench212014-09-30 08:27:02
JavaScript
doublench21, 2014-09-30 08:27:02

How to correctly process JSON data?

I have JSON data in the following format:

{
   0: "1",
   1: "0.39578100 1412014767",
   2: "755097411bb87a6a5894e39d9aac3bc4",
   3: "2",
   4: "0.43935900 1412014767",
   5: "2cab74f361f62e1f0bca2f593072e8db",
  /* ... */
}

I'm trying to add this data to html in the following tags, three elements each:
<tr id="1"><td>  </td><td>  </td><td>  </td></tr>
<tr id="2"><td>  </td><td>  </td><td>  </td></tr>
 <!-- ... -->

So far, there is only such a "meager":
$.getJSON(
            "server.php",
            {
                param1: static_rows
            },
            onAjaxSuccess
        );

        function onAjaxSuccess(data) {
            $.each(data, function(key, val){
                items.push(' ');
                $("#info").html(s);
            });


        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Petrov, 2014-09-30
@doublench21

1. You cannot start a property name with a number. In your case you need nested arrays of the form

{
  "t1": [
    "1",
    "0.39578100 1412014767",
    "755097411bb87a6a5894e39d9aac3bc4"
  ],
  "t2": [
    "2",
    "0.43935900 1412014767",
    "2cab74f361f62e1f0bca2f593072e8db"
  ]
}

2. You cannot start the values ​​of the id and name attributes with a number. And yes, numbers start with numbers. Names cannot start with a number. According to the code above:
<tr id="t1"><td></td><td></td><td></td></tr>
<tr id="t2"><td></td><td></td><td></td></tr>

3. Well, then just
function onAjaxSuccess(data) {
  $.each(data, function(key, rowData) {
    var cells = $('#' + key).find('td');

    $.each(rowData, function(index, value) {
      cells.eq(index).html(value);
    });
  });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question