JavaScript對Json的增刪改屬性詳解
來源:易賢網(wǎng) 閱讀:1118 次 日期:2016-06-16 17:09:47
溫馨提示:易賢網(wǎng)小編為您整理了“JavaScript對Json的增刪改屬性詳解”,方便廣大網(wǎng)友查閱!

使用JS對Json數(shù)據(jù)的處理,項目遇到需要對Json數(shù)據(jù)進行相關(guān)操作,比如增刪改操作,本以為會比較難,網(wǎng)上搜索下,發(fā)現(xiàn)還是比較簡單的,貼一段代碼:

代碼如下:

<script type="text/javascript">

    var json = {

      "age":24,

      "name":"cst"

    };

    //修改Json中的age值,因為Json中存在age屬性

    json["age"] = 30;

    alert(json.age); //30

    //增加Json中的sex值,因為Json中不存在sex屬性

    json["sex"] = "M";

    alert(json.sex); //M

    <!-- 遍歷Json中的數(shù)據(jù) -->

    for(var key in json){

      try{

        var value = eval("json['" + key +"']");

        alert(key+"_"+value);

      }catch(e){}

    }

    //刪除Json數(shù)據(jù)中的age屬性

    delete json["age"];

    alert(json.age); //undefined

  </script>

以上這篇JavaScript對Json的增刪改屬性詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:JavaScript對Json的增刪改屬性詳解
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點

版權(quán)所有:易賢網(wǎng)