JSON-如何从json字符串中获取单个内容[重复]

时间:2022-05-26 00:33:17

This question already has an answer here:

这个问题在这里已有答案:

hi i retrieve data as a JSON string like

我喜欢检索数据作为JSON字符串

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(1)”)。text(hllo)。

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

$(“tr [data-id = 1]> td:nth-​​child(2)”)。text(21)

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

$(“tr [data-id = 1]> td:nth-​​child(3)”)。text(45)

2 个解决方案

#1


1  

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

像这样: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)

另外,考虑从json中删除[],将'main'对象包装在一个对这个数据似乎无用的数组中。另外,建议不使用空格命名属性(将起始值更改为start_value)

#2


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.

在这里它可以帮助你。

#1


1  

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

像这样: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)

另外,考虑从json中删除[],将'main'对象包装在一个对这个数据似乎无用的数组中。另外,建议不使用空格命名属性(将起始值更改为start_value)

#2


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.

在这里它可以帮助你。