Android.Util.JsonWriter Class
Writes a JSON () encoded value to a stream, one token at a time.

See Also: JsonWriter Members

Syntax

[Android.Runtime.Register("android/util/JsonWriter", DoNotGenerateAcw=true)]
public sealed class JsonWriter : Java.Lang.Object, Java.IO.ICloseable, IDisposable

Remarks

Writes a JSON () encoded value to a stream, one token at a time. The stream includes both literal values (strings, numbers, booleans and nulls) as well as the begin and end delimiters of objects and arrays.

Encoding JSON

To encode your data as JSON, create a new JsonWriter. Each JSON document must contain one top-level array or object. Call methods on the writer as you walk the structure's contents, nesting arrays and objects as necessary:

Example

Suppose we'd like to encode a stream of messages such as the following:

java Example

[
   {
     "id": 912345678901,
     "text": "How do I write JSON on Android?",
     "geo": null,
     "user": {
       "name": "android_newb",
       "followers_count": 41
      
   },
   {
     "id": 912345678902,
     "text": "@android_newb just use android.util.JsonWriter!",
     "geo": [50.454722, -104.606667],
     "user": {
       "name": "jesse",
       "followers_count": 2
     }
   }
 ]}
This code encodes the above structure:

java Example

public void writeJsonStream(OutputStream out, List messages) throws IOException {
     JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));
     writer.setIndent("  ");
     writeMessagesArray(writer, messages);
     writer.close();
   

   public void writeMessagesArray(JsonWriter writer, List messages) throws IOException {
     writer.beginArray();
     for (Message message : messages) {
       writeMessage(writer, message);
     }
     writer.endArray();
   }

   public void writeMessage(JsonWriter writer, Message message) throws IOException {
     writer.beginObject();
     writer.name("id").value(message.getId());
     writer.name("text").value(message.getText());
     if (message.getGeo() != null) {
       writer.name("geo");
       writeDoublesArray(writer, message.getGeo());
     } else {
       writer.name("geo").nullValue();
     }
     writer.name("user");
     writeUser(writer, message.getUser());
     writer.endObject();
   }

   public void writeUser(JsonWriter writer, User user) throws IOException {
     writer.beginObject();
     writer.name("name").value(user.getName());
     writer.name("followers_count").value(user.getFollowersCount());
     writer.endObject();
   }

   public void writeDoublesArray(JsonWriter writer, List doubles) throws IOException {
     writer.beginArray();
     for (Double value : doubles) {
       writer.value(value);
     }
     writer.endArray();
   }}

Each JsonWriter may be used to write a single JSON stream. Instances of this class are not thread safe. Calls that would result in a malformed JSON string will fail with an Java.Lang.IllegalStateException.

[Android Documentation]

Requirements

Namespace: Android.Util
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 11