參考自d程序設計語言---個人博客http://my.oschina.net/u/218155/blog?fromerr=SwOkb7Sw fllow mejson
json庫的使用數組
import std.stdio; import std.json; void main() { //convert json string to json-value string s = "{\"name\":\"tom\",\"age\":12,\"grade\":\"[21,32,56,90]\"}"; JSONValue strToJV = parseJSON(s); writeln(strToJV["name"].str(),strToJV["age"].integer()); //convert json-value to json string // JSONValue[string] object;實際上對應一個JSONValue[string] 的關聯數組 JSONValue JVToStr = ["name":"tom"]; JVToStr.object["age"] = JSONValue(12); JVToStr.object["grade"] = JSONValue([21,32,56]); JVToStr.object["grade"].array ~= JSONValue(90); writeln(JVToStr.toString); }
D語言用JSONValue結構存儲json數據app
JSONValue經過union Store 存儲數據,type_tag存儲數據類型ide
union Store { string str; long integer; ulong uinteger; double floating; JSONValue[string] object; JSONValue[] array; } private Store store; private JSON_TYPE type_tag;
一堆的值判斷。
函數
JSONValue經過tostring方法調用toJSON方法調用ui
這個函數經過toValue去作判斷和處理spa
string toJSON(in JSONValue* root, in bool pretty = false, in JSONOptions options = JSONOptions.none) { auto json = appender!string(); void toString(string str) { json.put('"'); foreach (dchar c; str) { switch(c) { case '"': json.put("\\\""); break; case '\\': json.put("\\\\"); break; case '/': json.put("\\/"); break; case '\b': json.put("\\b"); break; case '\f': json.put("\\f"); break; case '\n': json.put("\\n"); break; case '\r': json.put("\\r"); break; case '\t': json.put("\\t"); break; default: appendJSONChar(&json, c, (msg) { throw new JSONException(msg); }); } } json.put('"'); } void toValue(in JSONValue* value, ulong indentLevel) { void putTabs(ulong additionalIndent = 0) { if(pretty) foreach(i; 0 .. indentLevel + additionalIndent) json.put(" "); } void putEOL() { if(pretty) json.put('\n'); } void putCharAndEOL(char ch) { json.put(ch); putEOL(); } final switch(value.type) { case JSON_TYPE.OBJECT: if(!value.store.object.length) { json.put("{}"); } else { putCharAndEOL('{'); bool first = true; void emit(R)(R names) { foreach (name; names) { auto member = value.store.object[name]; if(!first) putCharAndEOL(','); first = false; putTabs(1); toString(name); json.put(':'); if(pretty) json.put(' '); toValue(&member, indentLevel + 1); } } import std.algorithm : sort; auto names = value.store.object.keys; sort(names); emit(names); putEOL(); putTabs(); json.put('}'); } break; case JSON_TYPE.ARRAY: if(value.store.array.empty) { json.put("[]"); } else { putCharAndEOL('['); foreach (i, ref el; value.store.array) { if(i) putCharAndEOL(','); putTabs(1); toValue(&el, indentLevel + 1); } putEOL(); putTabs(); json.put(']'); } break; case JSON_TYPE.STRING: toString(value.store.str); break; case JSON_TYPE.INTEGER: json.put(to!string(value.store.integer)); break; case JSON_TYPE.UINTEGER: json.put(to!string(value.store.uinteger)); break; case JSON_TYPE.FLOAT: import std.math : isNaN, isInfinity; auto val = value.store.floating; if (val.isNaN) { if (options & JSONOptions.specialFloatLiterals) { toString(JSONFloatLiteral.nan); } else { throw new JSONException( "Cannot encode NaN. Consider passing the specialFloatLiterals flag."); } } else if (val.isInfinity) { if (options & JSONOptions.specialFloatLiterals) { toString((val > 0) ? JSONFloatLiteral.inf : JSONFloatLiteral.negativeInf); } else { throw new JSONException( "Cannot encode Infinity. Consider passing the specialFloatLiterals flag."); } } else { json.put(to!string(val)); } break; case JSON_TYPE.TRUE: json.put("true"); break; case JSON_TYPE.FALSE: json.put("false"); break; case JSON_TYPE.NULL: json.put("null"); break; } } toValue(root, 0); return json.data; }