JsonReader 类

定义

读取 JSON (RFC 4627) 编码值作为令牌流。

[Android.Runtime.Register("android/util/JsonReader", DoNotGenerateAcw=true)]
public sealed class JsonReader : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.IO.ICloseable
[<Android.Runtime.Register("android/util/JsonReader", DoNotGenerateAcw=true)>]
type JsonReader = class
    inherit Object
    interface ICloseable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
继承
JsonReader
属性
实现

注解

读取 JSON (RFC 4627) 编码值作为令牌流。 此流包括文本值 (字符串、数字、布尔值和 null) ,以及对象和数组的开始和结束分隔符。 令牌按深度优先顺序遍历,与它们在 JSON 文档中显示的顺序相同。 在 JSON 对象中,名称/值对由单个令牌表示。

<h3>分析 JSON</h3> 若要为自己的 JSON 流创建递归下降分析程序,请先创建一个用于创建 JsonReader的入口点方法。

接下来,为 JSON 文本中的每个结构创建处理程序方法。 对于每个对象类型和每个数组类型,都需要一个方法。 <ul>li 在强数组处理</强>方法中,首先调用 #beginArray 以使用数组的左括号。><>< 然后创建一个 while 循环来累积值,当 为 false 时 #hasNext 终止。 最后,通过调用 #endArray读取数组的右括号。 <li>在 <强>对象处理</强> 方法中,首先调用 #beginObject 以使用对象的左大括号。 然后,创建一个 while 循环,用于根据局部变量的名称向局部变量赋值。 当 为 false 时 #hasNext ,此循环应终止。 最后,通过调用 #endObject读取对象的右大括号。 </ul>

遇到嵌套对象或数组时,委托给相应的处理程序方法。

遇到未知名称时,严格分析程序应失败并出现异常。 Lenient 分析程序应调用 #skipValue() 以递归方式跳过值的嵌套令牌,否则这些令牌可能会发生冲突。

如果值可能为 null,应首先使用 #peek()检查。 可以使用 或 #skipValue()来使用 #nextNull() Null 文本。

<h3>示例</h3> 假设我们要分析消息流,如下所示:

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

此代码实现上述结构的分析程序:

{@code

              public List<Message> readJsonStream(InputStream in) throws IOException {
                JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
                try {
                  return readMessagesArray(reader);
                } finally {
                  reader.close();
                }
              }

              public List<Message> readMessagesArray(JsonReader reader) throws IOException {
                List<Message> messages = new ArrayList<Message>();

                reader.beginArray();
                while (reader.hasNext()) {
                  messages.add(readMessage(reader));
                }
                reader.endArray();
                return messages;
              }

              public Message readMessage(JsonReader reader) throws IOException {
                long id = -1;
                String text = null;
                User user = null;
                List<Double> geo = null;

                reader.beginObject();
                while (reader.hasNext()) {
                  String name = reader.nextName();
                  if (name.equals("id")) {
                    id = reader.nextLong();
                  } else if (name.equals("text")) {
                    text = reader.nextString();
                  } else if (name.equals("geo") && reader.peek() != JsonToken.NULL) {
                    geo = readDoublesArray(reader);
                  } else if (name.equals("user")) {
                    user = readUser(reader);
                  } else {
                    reader.skipValue();
                  }
                }
                reader.endObject();
                return new Message(id, text, user, geo);
              }

              public List<Double> readDoublesArray(JsonReader reader) throws IOException {
                List<Double> doubles = new ArrayList<Double>();

                reader.beginArray();
                while (reader.hasNext()) {
                  doubles.add(reader.nextDouble());
                }
                reader.endArray();
                return doubles;
              }

              public User readUser(JsonReader reader) throws IOException {
                String username = null;
                int followersCount = -1;

                reader.beginObject();
                while (reader.hasNext()) {
                  String name = reader.nextName();
                  if (name.equals("name")) {
                    username = reader.nextString();
                  } else if (name.equals("followers_count")) {
                    followersCount = reader.nextInt();
                  } else {
                    reader.skipValue();
                  }
                }
                reader.endObject();
                return new User(username, followersCount);
              }}

<h3>数字处理</h3> 此读取器允许将数值读取为字符串,字符串值可读取为数字。 例如,可以使用 或 #nextString读取 #nextInt JSON 数组[1, "1"]的两个元素。 此行为旨在防止有损数值转换:double 是 JavaScript 唯一的数值类型,而像这样的 9007199254740993 非常大的值不能在该平台上完全表示。 为了最大程度地减少精度损失,应以 JSON 格式的字符串的形式写入和读取非常大的值。

每个 JsonReader 都可用于读取单个 JSON 流。 此类的实例不是线程安全的。

android.util.JsonReaderJava 文档。

此页面的部分内容是基于 创建和共享的工作进行的修改,并根据 署名许可中所述的术语使用。

构造函数

JsonReader(Reader)

创建一个新实例,该实例从 in读取 JSON 编码的流。

属性

Class

返回此 Object的运行时类。

(继承自 Object)
Handle

基础 Android 实例的句柄。

(继承自 Object)
HasNext

如果当前数组或对象具有另一个元素,则返回 true。

JniIdentityHashCode

读取 JSON (RFC 4627) 编码值作为令牌流。

(继承自 Object)
JniPeerMembers

读取 JSON (RFC 4627) 编码值作为令牌流。

Lenient

如果此分析程序在接受的内容中是自由派的,则返回 true。 - 或 - 将此分析程序配置为自由接受的内容。

PeerReference

读取 JSON (RFC 4627) 编码值作为令牌流。

(继承自 Object)
ThresholdClass

此 API 支持 Mono for Android 基础结构,不应直接从代码使用。

(继承自 Object)
ThresholdType

此 API 支持 Mono for Android 基础结构,不应直接从代码使用。

(继承自 Object)

方法

BeginArray()

使用 JSON 流中的下一个令牌,并断言它是新数组的开头。

BeginArrayAsync()

读取 JSON (RFC 4627) 编码值作为令牌流。

BeginObject()

使用 JSON 流中的下一个令牌,并断言它是新对象的开头。

BeginObjectAsync()

读取 JSON (RFC 4627) 编码值作为令牌流。

Clone()

创建并返回此对象的副本。

(继承自 Object)
Close()

关闭此 JSON 读取器和基础 Reader

Dispose()

读取 JSON (RFC 4627) 编码值作为令牌流。

(继承自 Object)
Dispose(Boolean)

读取 JSON (RFC 4627) 编码值作为令牌流。

(继承自 Object)
EndArray()

使用 JSON 流中的下一个令牌,并断言它是当前数组的末尾。

EndArrayAsync()

读取 JSON (RFC 4627) 编码值作为令牌流。

EndObject()

使用 JSON 流中的下一个令牌,并断言它是当前对象的末尾。

EndObjectAsync()

读取 JSON (RFC 4627) 编码值作为令牌流。

Equals(Object)

指示其他某个对象是否“等于”此对象。

(继承自 Object)
GetHashCode()

返回对象的哈希代码值。

(继承自 Object)
JavaFinalize()

当垃圾回收确定不再引用对象时,由对象上的垃圾回收器调用。

(继承自 Object)
NextBoolean()

返回 JsonToken#BOOLEAN boolean 下一个令牌的值,并使用它。

NextBooleanAsync()

读取 JSON (RFC 4627) 编码值作为令牌流。

NextDouble()

返回 JsonToken#NUMBER double 下一个令牌的值,并使用它。

NextDoubleAsync()

读取 JSON (RFC 4627) 编码值作为令牌流。

NextInt()

返回 JsonToken#NUMBER int 下一个令牌的值,并使用它。

NextIntAsync()

读取 JSON (RFC 4627) 编码值作为令牌流。

NextLong()

返回 JsonToken#NUMBER long 下一个令牌的值,并使用它。

NextLongAsync()

读取 JSON (RFC 4627) 编码值作为令牌流。

NextName()

返回下一个令牌 , JsonToken#NAME property name并使用它。

NextNameAsync()

读取 JSON (RFC 4627) 编码值作为令牌流。

NextNull()

使用 JSON 流中的下一个令牌,并断言它是文本 null。

NextNullAsync()

读取 JSON (RFC 4627) 编码值作为令牌流。

NextString()

返回 JsonToken#STRING string 下一个令牌的值,并使用它。

NextStringAsync()

读取 JSON (RFC 4627) 编码值作为令牌流。

Notify()

唤醒正在等待此对象的监视器的单个线程。

(继承自 Object)
NotifyAll()

唤醒正在等待此对象的监视器的所有线程。

(继承自 Object)
Peek()

返回下一个令牌的类型,而不使用它。

PeekAsync()

读取 JSON (RFC 4627) 编码值作为令牌流。

SetHandle(IntPtr, JniHandleOwnership)

设置 Handle 属性。

(继承自 Object)
SkipValue()

以递归方式跳过下一个值。

SkipValueAsync()

读取 JSON (RFC 4627) 编码值作为令牌流。

ToArray<T>()

读取 JSON (RFC 4627) 编码值作为令牌流。

(继承自 Object)
ToString()

返回对象的字符串表示形式。

(继承自 Object)
UnregisterFromRuntime()

读取 JSON (RFC 4627) 编码值作为令牌流。

(继承自 Object)
Wait()

导致当前线程等待,直到它被唤醒,通常是通过 em <通知/em> 或 <em>interrupted</em>。<>

(继承自 Object)
Wait(Int64)

导致当前线程等待,直到它被唤醒,通常是通过 em <通知/em> 或 <em>interrupted</em>,或直到经过一定数量的实时。<>

(继承自 Object)
Wait(Int64, Int32)

导致当前线程等待,直到它被唤醒,通常是通过 em <通知/em> 或 <em>interrupted</em>,或直到经过一定数量的实时。<>

(继承自 Object)

显式接口实现

IJavaPeerable.Disposed()

读取 JSON (RFC 4627) 编码值作为令牌流。

(继承自 Object)
IJavaPeerable.DisposeUnlessReferenced()

读取 JSON (RFC 4627) 编码值作为令牌流。

(继承自 Object)
IJavaPeerable.Finalized()

读取 JSON (RFC 4627) 编码值作为令牌流。

(继承自 Object)
IJavaPeerable.JniManagedPeerState

读取 JSON (RFC 4627) 编码值作为令牌流。

(继承自 Object)
IJavaPeerable.SetJniIdentityHashCode(Int32)

读取 JSON (RFC 4627) 编码值作为令牌流。

(继承自 Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates)

读取 JSON (RFC 4627) 编码值作为令牌流。

(继承自 Object)
IJavaPeerable.SetPeerReference(JniObjectReference)

读取 JSON (RFC 4627) 编码值作为令牌流。

(继承自 Object)

扩展方法

JavaCast<TResult>(IJavaObject)

执行 Android 运行时检查的类型转换。

JavaCast<TResult>(IJavaObject)

读取 JSON (RFC 4627) 编码值作为令牌流。

GetJniTypeName(IJavaPeerable)

读取 JSON (RFC 4627) 编码值作为令牌流。

适用于