[转]Java Code Examples for android.util.JsonReader

时间:2023-03-08 16:30:53

【转】Java Code Examples for android.util.JsonReader

The following are top voted examples for showing how to use android.util.JsonReader. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to product more good examples.

Example 1
Project: Earmouse   File: ModuleStats.java View source code   7 votes
/**
* Reads an existing ModuleStats instance from a JSON file
* @param fr The FileReader to read from
* @throws IOException
*/
private void initModuleStatsFromJson(FileReader fr) throws IOException {
JsonReader reader = new JsonReader(fr); reader.beginArray();
while (reader.hasNext()) {
reader.beginObject();
int exerciseIndex = -1;
boolean result = true;
long timestamp = -1; while (reader.hasNext()){
String name = reader.nextName();
switch (name) {
case "exerciseIndex":
exerciseIndex = reader.nextInt();
break;
case "result":
result = reader.nextBoolean();
break;
case "timestamp":
timestamp = reader.nextLong();
break;
default:
reader.skipValue();
break;
}
}
moduleAnswerList.add(new ModuleAnswer(exerciseIndex, result, timestamp));
reader.endObject(); }
reader.endArray();
reader.close();
}
Example 2
Project: deck_old   File: CardHolder.java View source code Vote up 6 votes
public static CardHolder readFromJson( JsonReader reader ) throws IOException
{
CardHolder player = new CardHolder(); reader.beginObject();
while( reader.hasNext() )
{
String name = reader.nextName();
if( name.equals( JSON_ID ) )
{
player.mID = reader.nextString();
}
else if( name.equals( JSON_NAME ) )
{
player.mName = reader.nextString();
}
else if( name.equals( JSON_CARDS ) && reader.peek() != JsonToken.NULL )
{
reader.beginArray();
while( reader.hasNext() )
{
player.addCard( Card.readFromJson( reader ) );
}
reader.endArray();
}
else
{
reader.skipValue();
}
}
reader.endObject(); return player;
}
Example 3
Project: Earmouse   File: Module.java View source code Vote up 5 votes
/**
* Loads this Module's properties and data from the JSON data of the given Reader
* @param r The Reader from which to read the JSON data
* @throws IOException
*/
private void initModuleFromJson(Reader r) throws IOException {
JsonReader reader = new JsonReader(r); reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
switch (name) {
case "moduleId":
this.id = reader.nextInt();
break;
case "title":
this.title = reader.nextString();
break;
case "description":
this.description = reader.nextString();
break;
case "shortDescription":
this.shortDescription = reader.nextString();
break;
case "lowestNote":
this.lowestNote = reader.nextInt();
break;
case "highestNote":
this.highestNote = reader.nextInt();
break;
case "difficulty":
this.difficulty = reader.nextInt();
break;
case "version":
this.toolVersion = reader.nextString();
break;
case "moduleVersion":
this.moduleVersion = reader.nextInt();
break;
case "exerciseList":
reader.beginArray();
for (int i = 0; reader.hasNext(); i++) {
this.exerciseList.add(new Exercise());
reader.beginArray();
for (int j = 0; reader.hasNext(); j++) {
this.exerciseList.get(i).exerciseUnits.add(new ArrayList<Integer>());
reader.beginArray();
while (reader.hasNext()) {
this.exerciseList.get(i).exerciseUnits.get(j).add(reader.nextInt());
}
reader.endArray();
}
reader.endArray();
}
reader.endArray();
break;
case "answerList":
reader.beginArray();
while (reader.hasNext()) {
this.answerList.add(reader.nextString());
}
reader.endArray();
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
reader.close();
}