【问题标题】:Error adding a json object in a json localstorage在 json localstorage 中添加 json 对象时出错
【发布时间】:2013-11-13 02:24:31
【问题描述】:

我正在尝试在其他 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));
    }

【问题讨论】:

    标签: javascript json local-storage


    【解决方案1】:

    您尝试在 shops 对象上调用 push 而不是在 c 数组上,您可以这样做:

    var oldCli = JSON.parse(localStorage.getItem('shops')).c || [];  
    

    【讨论】:

    • 嗨,阿尔贝托,再次感谢。这次我尝试将 2 个 json 保存在另一个 json 中,例如:___________________________________ { "c": [ { "cliente_id": "1", "cliente_name": "Quebracho 1", }, { "cliente_id": "2", "cliente_name": "Malinche 1" } ] "c": [ { "cliente_id": "1", "cliente_name": "Quebracho 2" }, { "cliente_id": "2", "cliente_name" : "马林奇 2" } ] }
    • 要么更新你的问题,要么创建一个新问题,这样很难理解你在做什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 2014-03-29
    • 2021-11-12
    • 1970-01-01
    相关资源
    最近更新 更多