我有一个如下的本地存储阵列

pageItems  = {"stationID":"145","categoryID":"-1","pickupDate":"2014-04-22","pickupTime":"08:00","returnDate":"2014-04-23","returnTime":"08:00","milage":"false","miles":"","attributes":[[],[]],"additionals":[[],[]]}

我需要将categoryID值更新为65

pageItems  = {"stationID":"145","categoryID":"65","pickupDate":"2014-04-22","pickupTime":"08:00","returnDate":"2014-04-23","returnTime":"08:00","milage":"false","miles":"","attributes":[[],[]],"additionals":[[],[]]}

===============>>#1 票数:7

尝试这个

var pageItems = {
            "stationID": "145",
            "categoryID": "-1",
            "pickupDate": "2014-04-22",
            "pickupTime": "08:00",
            "returnDate": "2014-04-23",
            "returnTime": "08:00",
            "milage": "false",
            "miles": "",
            "attributes": [[], []],
            "additionals": [[], []]
};

function setObject(key, obj) {
    localStorage.setItem(key, JSON.stringify(obj));
}

function getObject(key) {
    return JSON.parse(localStorage.getItem(key));
}

function updateItem(key, property, value)
{
    var obj = getObject(key);
    obj[property] = value;    
    setObject(key, obj);
}

//set object to sessionStorage
setObject('test', pageItems);

//update object property in sessionStorage
updateItem('test', 'categoryID', 65);

//get object from sessionStorage
getObject('test');

这是演示

===============>>#2 票数:2 已采纳

您可以使用以下内容;

var pageItems  = '{"stationID":"145","categoryID":"-1","pickupDate":"2014-04-22","pickupTime":"08:00","returnDate":"2014-04-23","returnTime":"08:00","milage":"false","miles":"","attributes":[[],[]],"additionals":[[],[]]}';

var json = JSON.parse(pageItems);

json.categoryID = 65;

pageItems_v2 = JSON.stringify(json);

这是一个工作示例: 演示

===============>>#3 票数:0

你可以用像

pageItems.categoryID=65;

小提琴

  ask by nishi translate from so

未解决问题?本站智能推荐: