2016-09-06 174 views
0

單內容,您好我檢索數據作爲JSON字符串像JSON-如何從一個JSON字符串

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

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

我要怎樣才能讓我的專欄,內容爲:

$("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)

+0

'data [0] .main [「start value」]'會取你'21' – Satpal

+0

檢查這個[demo](https://jsfiddle.net/xLofv5oL/1) – guradio

+0

這可能會幫助你 http: //stackoverflow.com/questions/20813223/jquery-how-to-get-a-row-of-json-data-from-the-json-data-list-by-id-etc –

回答

1

像這樣: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']) 

此外,考慮刪除[]從JSON,它包裝 '主' 客體在這似乎無用與陣列這個數據。此外,命名的屬性沒有空格建議(改變start valuestart_value

+0

但顯示錯誤「無法正確讀取'tittle'」未定義 –

+0

檢查小提琴:https://jsfiddle.net/d5qe70bw/。您必須將您的json分配給mr_cools_data。 –

+0

實際上,我的數據正在獲得類似成功的功能:function(data){ debugger; (數據) –

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>

這可以幫助你。

+0

@ sachin GetPageInfoResult對我來說不是一個變量我的變量是jsonPretty –

+0

哦!你不改變我的JSON字符串。我不能改變它 –

+0

沒有幫助你嗎? –