0

我正在尝试在其他 json 对象中添加一个 json,存储在本地存储中,但出现错误 ¨: Object # has no method 'push'

本地存储对象为:

shops {
        "c": [
            {
                "cliente_id": "12",
                "cliente_name": "Vermut",
                "cliente_url": "vermut",
                "categoria_name": "Restaurantes",
                "imagen": "1364.jpg",
                "telefono": "456987123",
                "descripcion": "El mejor sitio en el centro",
                "latitud": "41.3865749",
                "longitud": "2.1745545999999",
                "direccion": "Carrera",
                "porcentaje": "50",
                "distancia": "0.0764"
            },
            {
                "cliente_id": "92",
                "cliente_name": "mexicano",
                "cliente_url": "mexicano",
                "categoria_name": "Restaurantes",
                "imagen": "135.jpg",
                "telefono": "",
                "descripcion": "Comida a\r\n\r\nNuestros . \r\n",
                "latitud": "3.9999",
                "longitud": "6.9999",
                "direccion": "Mexicana",
                "porcentaje": "50",
                "distancia": "0.771095"
            }
        ]
    }

这是我尝试将 json 放在另一个中的代码。我做错了什么?

    function addItem() {
    //I retrieve the old object
        var oldCli = JSON.parse(localStorage.getItem('shops')) || [];  
        var newCli = {
        "c": [
            {
                "cliente_id": "1",
                "cliente_name": "Restaurante Quebracho",
                "cliente_url": "restaurante-quebracho",
                "categoria_name": "Restaurantes",
                "imagen": "1355.jpg",
                "telefono": "123654469",
                "descripcion": "Quebrachos",
                "latitud": "5.3456",
                "longitud": "2.1264",
                "direccion": "Ronda Quebracho",
                "porcentaje": "25",
                "distancia": "0.4273"
            },
            {
                "cliente_id": "2",
                "cliente_name": "Malinche",
                "cliente_url": "disco-malinche",
                "categoria_name": "Discotecas",
                "imagen": "13493.png",
                "telefono": "234567",
                "descripcion": "Malinches.",
                "latitud": "6.98765",
                "longitud": "1.23456",
                "direccion": "C Malinche 42",
                "porcentaje": "30",
                "distancia": "1.0994"
            }
        ]
    };

        oldCli.push(newCli);

        localStorage.setItem('shops', JSON.stringify(oldCli));
    }
4

1 回答 1

1

您正在尝试调用不在数组push上的对象,您可以这样做:shopsc

var oldCli = JSON.parse(localStorage.getItem('shops')).c || [];  
于 2013-08-05T11:31:39.237 回答