 <p>Here's a simple JSON encoder in Qt -- should be relatively easy to recast into Java. All you really need to do is to make sure the keys are sorted when writing out -- can read in with another JSON package.</p> <pre><code>QString QvJson::encodeJson(const QVariant&amp; jsonObject) { QVariant::Type type = jsonObject.type(); switch (type) { case QVariant::Map: return encodeObject(jsonObject); case QVariant::List: return encodeArray(jsonObject); case QVariant::String: return encodeString(jsonObject); case QVariant::Int: case QVariant::Double: return encodeNumeric(jsonObject); case QVariant::Bool: return encodeBool(jsonObject); case QVariant::Invalid: return encodeNull(jsonObject); default: return encodingError("encodeJson", jsonObject, ErrorUnrecognizedObject); } } QString QvJson::encodeObject(const QVariant&amp; jsonObject) { QString result("{ "); QMap&lt;QString, QVariant&gt; map = jsonObject.toMap(); QMapIterator&lt;QString, QVariant&gt; i(map); while (i.hasNext()) { i.next(); result.append(encodeString(i.key())); result.append(" : "); result.append(encodeJson(i.value())); if (i.hasNext()) { result.append(", "); } } result.append(" }"); return result; } QString QvJson::encodeArray(const QVariant&amp; jsonObject) { QString result("[ "); QList&lt;QVariant&gt; list = jsonObject.toList(); for (int i = 0; i &lt; list.count(); i++) { result.append(encodeJson(list.at(i))); if (i+1 &lt; list.count()) { result.append(", "); } } result.append(" ]"); return result; } QString QvJson::encodeString(const QVariant &amp;jsonObject) { return encodeString(jsonObject.toString()); } QString QvJson::encodeString(const QString&amp; value) { QString result = "\""; for (int i = 0; i &lt; value.count(); i++) { ushort chr = value.at(i).unicode(); if (chr &lt; 32) { switch (chr) { case '\b': result.append("\\b"); break; case '\f': result.append("\\f"); break; case '\n': result.append("\\n"); break; case '\r': result.append("\\r"); break; case '\t': result.append("\\t"); break; default: result.append("\\u"); result.append(QString::number(chr, 16).rightJustified(4, '0')); } // End switch } else if (chr &gt; 255) { result.append("\\u"); result.append(QString::number(chr, 16).rightJustified(4, '0')); } else { result.append(value.at(i)); } } result.append('"'); QString displayResult = result; // For debug, since "result" often doesn't show Q_UNUSED(displayResult); return result; } QString QvJson::encodeNumeric(const QVariant&amp; jsonObject) { return jsonObject.toString(); } QString QvJson::encodeBool(const QVariant&amp; jsonObject) { return jsonObject.toString(); } QString QvJson::encodeNull(const QVariant&amp; jsonObject) { return "null"; } QString QvJson::encodingError(const QString&amp; method, const QVariant&amp; jsonObject, Error error) { QString text; switch (error) { case ErrorUnrecognizedObject: text = QObject::tr("Unrecognized object type"); break; default: Q_ASSERT(false); } return QObject::tr("*** Error %1 in QvJson::%2 -- %3").arg(error).arg(method).arg(text); } </code></pre>