Heim  >  Fragen und Antworten  >  Hauptteil

So löschen Sie ein Objekt, wenn alle Elemente in JSON mit Javascript null sind

Können Sie mir sagen, wie ich mit Javascript alle Nullobjekte in JSON entfernen kann?

Ich muss auch verschachtelte Objekte mit Null-/leeren Schlüsseln entfernen.

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
              "text": null,
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                         "definition": null
                    },
                    "GlossSee": "markup",
                    "window": {
                        "title": "Sample Konfabulator Widget",
                        "description": ""
                    }
                }
            }
        },
        "image": {
            "src": null,
            "name": null,
            "alignment": null
        },
        "text": {
            "data": "Click Here",
            "size": null,
            "style": "bold",
            "name": "text1",
            "hOffset": "",
            "vOffset": "",
            "alignment": "center",
            "onMouseUp": null
        }
    }
}

Die erforderliche Ausgabe lautet wie folgt:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook."
                    },
                    "GlossSee": "markup",
                    "window": {
                        "title": "Sample Konfabulator Widget"
                    }
                }
            }
        },
        "text": {
            "data": "Click Here",
            "style": "bold",
            "name": "text1",
            "alignment": "center"
        }
    }
}

So löschen Sie rekursiv Objekte mit Null- oder leeren Schlüsseln im gesamten JSON. Genau wie das image-Objekt haben seine Schlüssel Null- oder Nullwerte.

P粉575055974P粉575055974382 Tage vor595

Antworte allen(1)Ich werde antworten

  • P粉311464935

    P粉3114649352023-09-09 10:24:59

    您可以在 JSON.stringify(value, Replacer) / JSON.parse 中使用 replacer/reviver 获得更接近的结果(文本,复活者)

    使用 JSON.stringify 的示例

    let data = {"glossary":{"title":"example glossary","GlossDiv":{"title":"S","text":null,"GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","definition":null},"GlossSee":"markup","window":{"title":"Sample Konfabulator Widget","description":""}}}},"image":{"src":null,"name":null,"alignment":null},"text":{"data":"Click Here","size":null,"style":"bold","name":"text1","hOffset":"","vOffset":"","alignment":"center","onMouseUp":null}}}
    
    let json = JSON.stringify(data, (key, value) => {
        return (value === null || value === '') ? undefined : value
    }, 4)
    
    console.log(json)

    Antwort
    0
  • StornierenAntwort