JsonReader Classe
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Legge un valore con codifica JSON (RFC 4627) come flusso di token.
[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
- Ereditarietà
- Attributi
- Implementazioni
Commenti
Legge un valore con codifica JSON (RFC 4627) come flusso di token. Questo flusso include sia i valori letterali (stringhe, numeri, booleani e null) sia i delimitatori di inizio e fine di oggetti e matrici. I token vengono attraversati in ordine approfondito, lo stesso ordine in cui vengono visualizzati nel documento JSON. All'interno di oggetti JSON, le coppie nome/valore sono rappresentate da un singolo token.
<h3>Analisi JSON</h3> Per creare un parser di discesa ricorsivo per i flussi JSON personalizzati, creare prima di tutto un metodo del punto di ingresso che crea un oggetto JsonReader
.
Creare quindi i metodi del gestore per ogni struttura nel testo JSON. È necessario un metodo per ogni tipo di oggetto e per ogni tipo di matrice. <ul li Within strong array handling/strong methods, first call to consume the array's opening bracket.ul><li>Within <strong>array handling</strong> methods, first call #beginArray
to consume the array's opening bracket. Creare quindi un ciclo while che accumula valori, terminando quando #hasNext
è false. Infine, leggere la parentesi chiusa della matrice chiamando #endArray
. <li Within strong object handling/strong methods, first call to consume the object's opening brace.li>Within <strong>object handling</strong> methods, first call #beginObject
to consume the object's opening brace. Creare quindi un ciclo while che assegna valori alle variabili locali in base al nome. Questo ciclo deve terminare quando #hasNext
è false. Infine, leggere la parentesi graffa di chiusura dell'oggetto chiamando #endObject
. </ul>
Quando viene rilevato un oggetto o una matrice nidificata, delegare al metodo del gestore corrispondente.
Quando viene rilevato un nome sconosciuto, i parser rigorosi devono avere esito negativo con un'eccezione. I parser lenienti devono chiamare #skipValue()
per ignorare in modo ricorsivo i token annidati del valore, che potrebbero altrimenti essere in conflitto.
Se un valore può essere Null, è prima necessario verificare usando #peek()
. I valori letterali Null possono essere utilizzati usando #nextNull()
o #skipValue()
.
<h3>Esempio</h3> Si supponga di voler analizzare un flusso di messaggi, ad esempio:
{@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
}
}
]}
Questo codice implementa il parser per la struttura precedente:
{@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>Gestione< numeri/h3> Questo lettore consente di leggere i valori numerici come stringhe e valori stringa come numeri. Ad esempio, entrambi gli elementi della matrice [1, "1"]
JSON possono essere letti usando #nextInt
o #nextString
. Questo comportamento è progettato per evitare conversioni numeriche con perdita: double è l'unico tipo numerico di JavaScript e valori molto grandi come 9007199254740993
non possono essere rappresentati esattamente su tale piattaforma. Per ridurre al minimo la perdita di precisione, i valori estremamente grandi devono essere scritti e letti come stringhe in JSON.
Ognuno di essi JsonReader
può essere usato per leggere un singolo flusso JSON. Le istanze di questa classe non sono thread-safe.
Documentazione java per android.util.JsonReader
.
Le parti di questa pagina sono modifiche basate sul lavoro creato e condiviso dal Android e usato in base ai termini descritti in 2.5 Attribution License Creative Commons 2.5 Attribution License.
Costruttori
JsonReader(Reader) |
Crea una nuova istanza che legge un flusso con codifica JSON da |
Proprietà
Class |
Restituisce la classe di runtime di questo |
Handle |
Handle per l'istanza di Android sottostante. (Ereditato da Object) |
HasNext |
Restituisce true se la matrice o l'oggetto corrente ha un altro elemento. |
JniIdentityHashCode |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. (Ereditato da Object) |
JniPeerMembers |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |
Lenient |
Restituisce true se questo parser è liberale in quello che accetta. -oppure- Configurare questo parser in modo che sia liberale in ciò che accetta. |
PeerReference |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. (Ereditato da Object) |
ThresholdClass |
Questa API supporta l'infrastruttura Mono per Android e non deve essere usata direttamente dal codice. (Ereditato da Object) |
ThresholdType |
Questa API supporta l'infrastruttura Mono per Android e non deve essere usata direttamente dal codice. (Ereditato da Object) |
Metodi
BeginArray() |
Usa il token successivo dal flusso JSON e afferma che si tratta dell'inizio di una nuova matrice. |
BeginArrayAsync() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |
BeginObject() |
Utilizza il token successivo dal flusso JSON e afferma che si tratta dell'inizio di un nuovo oggetto. |
BeginObjectAsync() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |
Clone() |
Crea e restituisce una copia di questo oggetto. (Ereditato da Object) |
Close() |
Chiude questo lettore JSON e l'oggetto sottostante |
Dispose() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. (Ereditato da Object) |
Dispose(Boolean) |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. (Ereditato da Object) |
EndArray() |
Usa il token successivo dal flusso JSON e afferma che è la fine della matrice corrente. |
EndArrayAsync() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |
EndObject() |
Utilizza il token successivo dal flusso JSON e afferma che è la fine dell'oggetto corrente. |
EndObjectAsync() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |
Equals(Object) |
Indica se un altro oggetto è "uguale a" questo. (Ereditato da Object) |
GetHashCode() |
Restituisce un valore del codice hash per l'oggetto. (Ereditato da Object) |
JavaFinalize() |
Chiamato dal Garbage Collector su un oggetto quando Garbage Collection determina che non sono presenti altri riferimenti all'oggetto . (Ereditato da Object) |
NextBoolean() |
Restituisce il |
NextBooleanAsync() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |
NextDouble() |
Restituisce il |
NextDoubleAsync() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |
NextInt() |
Restituisce il |
NextIntAsync() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |
NextLong() |
Restituisce il |
NextLongAsync() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |
NextName() |
Restituisce il token successivo, un |
NextNameAsync() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |
NextNull() |
Utilizza il token successivo dal flusso JSON e afferma che si tratta di un valore letterale Null. |
NextNullAsync() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |
NextString() |
Restituisce il |
NextStringAsync() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |
Notify() |
Riattiva un singolo thread in attesa del monitor dell'oggetto. (Ereditato da Object) |
NotifyAll() |
Riattiva tutti i thread in attesa del monitoraggio di questo oggetto. (Ereditato da Object) |
Peek() |
Restituisce il tipo del token successivo senza utilizzarlo. |
PeekAsync() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |
SetHandle(IntPtr, JniHandleOwnership) |
Imposta la proprietà Handle. (Ereditato da Object) |
SkipValue() |
Ignora il valore successivo in modo ricorsivo. |
SkipValueAsync() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |
ToArray<T>() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. (Ereditato da Object) |
ToString() |
Restituisce una rappresentazione in formato stringa dell'oggetto. (Ereditato da Object) |
UnregisterFromRuntime() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. (Ereditato da Object) |
Wait() |
Fa sì che il thread corrente attenda finché non viene risvegliato, in genere eseguendo <una>notifica</em> o <em>interrotto</em>. (Ereditato da Object) |
Wait(Int64) |
Fa sì che il thread corrente attenda finché non viene risvegliato, in genere eseguendo <una notifica</>em> o <em>interrotto</em> o fino a quando non è trascorsa una certa quantità di tempo reale. (Ereditato da Object) |
Wait(Int64, Int32) |
Fa sì che il thread corrente attenda finché non viene risvegliato, in genere eseguendo <una notifica</>em> o <em>interrotto</em> o fino a quando non è trascorsa una certa quantità di tempo reale. (Ereditato da Object) |
Implementazioni dell'interfaccia esplicita
IJavaPeerable.Disposed() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. (Ereditato da Object) |
IJavaPeerable.DisposeUnlessReferenced() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. (Ereditato da Object) |
IJavaPeerable.Finalized() |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. (Ereditato da Object) |
IJavaPeerable.JniManagedPeerState |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. (Ereditato da Object) |
IJavaPeerable.SetJniIdentityHashCode(Int32) |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. (Ereditato da Object) |
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. (Ereditato da Object) |
IJavaPeerable.SetPeerReference(JniObjectReference) |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. (Ereditato da Object) |
Metodi di estensione
JavaCast<TResult>(IJavaObject) |
Esegue una conversione del tipo verificato dal runtime Android. |
JavaCast<TResult>(IJavaObject) |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |
GetJniTypeName(IJavaPeerable) |
Legge un valore con codifica JSON (RFC 4627) come flusso di token. |