技術(shù)頻道導(dǎo)航
HTML/CSS
.NET技術(shù)
IIS技術(shù)
PHP技術(shù)
Js/JQuery
Photoshop
Fireworks
服務(wù)器技術(shù)
操作系統(tǒng)
網(wǎng)站運(yùn)營(yíng)

贊助商

分類目錄

贊助商

最新文章

搜索

JQuery如何把JSON字符串轉(zhuǎn)為JSON對(duì)象

作者:admin    時(shí)間:2019-8-29 16:26:44    瀏覽:

本文介紹JQuery如何把JSON字符串轉(zhuǎn)為JSON對(duì)象。

HTML代碼

下面的HTML代碼實(shí)現(xiàn)JQuery把JSON字符串轉(zhuǎn)為JSON對(duì)象。

<html>
<head>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<form id="form1">
  <input type = "button" id = "demo" value = "Demo" />
  <br />
  <div>
    Name: <input type = "text" id = "txtName" /><br />
    Age: <input type = "text" id = "txtAge" /><br />
    City: <input type = "text" id = "txtCity" /><br />
    Country: <input type = "text" id = "txtCountry" /><br />
  </div>
</form>

<script type = "text/javascript">
  var json = "{Name: 'Mudassar Khan', Age: 27, City: 'Mumbai', Country: 'India'}";
  $("#demo").on("click", function () {
    var person = eval('(' +json + ')');
    $("#txtName").val(person.Name);
    $("#txtAge").val(person.Age);
    $("#txtCity").val(person.City);
    $("#txtCountry").val(person.Country);
  });
</script>

</body>
</html>
 

效果如圖:

JSON字符串轉(zhuǎn)為JSON對(duì)象

JSON字符串轉(zhuǎn)為JSON對(duì)象

解釋

在上面的代碼片段中,當(dāng)單擊HTML Demo按鈕時(shí),JSON字符串將使用javascript eval()函數(shù)轉(zhuǎn)換為JSON對(duì)象,然后JSON對(duì)象值將顯示在各自的文本框中。

execcodegetcode

標(biāo)簽: JSON  JQuery  
x
  • 站長(zhǎng)推薦
/* 左側(cè)顯示文章內(nèi)容目錄 */