2016-05-20 8 views
-1

wie formatiert ein stringed JSON-Objekt?JSON.Stringify mit Absicht

JSON.stringify({a: 'avalue', b: 'bvalue'}); 

Zur Ausgabe dieser:

{ 
    a: 'valuea', 
    b: 'valueb' 
} 

statt dessen:

{a:'valuea', b: 'valueb'} 
+1

bereits beantwortet [hier] (http://stackoverflow.com/questions/12950375/javascript-object-to-formatted-string/12951419#12951419) – mdickin

+1

Würde nicht lesen Die Dokumente für 'JSON.stringify' sind normalerweise der erste Schritt? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify –

+0

['[javascript] json indentation'] (https://stackoverflow.com/search?q= % 5Bjavascript% 5D + json + Einzug) –

Antwort

1

JSON.stringify Funktion nimmt 3 parameters. Der dritte definiert, wie viele Leerzeichen für den Einzug eingefügt werden. Damit Sie es verwenden können:

JSON.stringify({a: 'avalue', b: 'bvalue'}, null, 2); 
+0

Thx!, Das ist, was ich gesucht habe :) –

Verwandte Themen