0

hi i retrieve data as a JSON string like

var jsonPretty = JSON.stringify(data, null, 2);

{
    "GetPageInfoResult": [{
        "main": {
            "sub": [],
            "tittle": "hllo",
            "startvalue": "21",
            "stopvalue": "45",
            "status": "",
            "accumalated": "",
            "comment": ""
        }
    }]
}

how can i get my my column content as:

$("tr[data-id=1] > td:nth-child(1)").text(hllo).

$("tr[data-id=1] > td:nth-child(2)").text(21)

$("tr[data-id=1] > td:nth-child(3)").text(45)

Manu Padmanabhan
  • 555
  • 1
  • 4
  • 16

2 Answers2

1

Like this: https://jsfiddle.net/d5qe70bw/

var mr_cools_data = [{
    "main": {
        "sub": [],
        "tittle": "water",
        "start value": "21",
        "stop value": "45",
        "status": "",
        "accumulated": "",
        "comment": ""
    }
}]

$("tr[data-id=1] > td:nth-child(1)").text(mr_cools_data[0].main['tittle'])

$("tr[data-id=1] > td:nth-child(2)").text(mr_cools_data[0].main['start value'])

$("tr[data-id=1] > td:nth-child(3)").text(mr_cools_data[0].main['stop value'])

Also, consider removing the [] from the json, that wraps the 'main' object in an array which seems useless with this data. Also, naming the attributes without whitespace is recommended(change start value to start_value)

Martin Gottweis
  • 2,721
  • 13
  • 27
1

var jsonPretty  = [{
    "main": {
        "sub": [],
         "tittle": "hllo",
            "startvalue": "",
            "stopvalue": "",
            "status": "",
            "accumalated": "",
            "comment": ""
    }
}]

$("tr[data-id=1] > td:nth-child(1)").text(jsonPretty [0].main['tittle']);

$("tr[data-id=1] > td:nth-child(2)").text(jsonPretty [0].main['start value']);

$("tr[data-id=1] > td:nth-child(3)").text(jsonPretty [0].main['stop value']);

$("tr[data-id=1] > td:nth-child(4)").text(jsonPretty [0].main['status']);

$("tr[data-id=1] > td:nth-child(5)").text(jsonPretty [0].main['accumulated']);

$("tr[data-id=1] > td:nth-child(6)").text(jsonPretty [0].main['comment']);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr data-id="1">
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>

Here it may help you out.

Sachin Bahukhandi
  • 2,378
  • 20
  • 29