I have a problem of codification reading a local .json from raw folder. The ParseJson class is same to example official: http://developer.android.com/reference/android/util/JsonReader.html
I pass it a InputSteam and I get a List correctly but with wrong characters (´ 1/4 etc.) and I used UTF-8 into ParseJson.
Will be the error in my inputStream? Should I encode in UTF-8 my InputStream before to pass it to ParseJson?
entrantes.json
[
{
"foto": "ajocolorao";
"nombre": "Ajocolorao",
"pueblo":"Alfarnatejo",
"ingredientes": "(4 personas)\n -¼ kg de bacalao\n -El zumo de ½ limón"
}
]
Getting the .json
List<Receta> listRecetas = new ArrayList<Receta>();
InputStream inpuStream = getResources().openRawResource(R.raw.entrantes);
ParseJson parse = new ParseJson();
listRecetas = parse.readJsonStream(inputStream);
ParseJson.java
public class ParseJson {
public List<Receta> readJsonStream(InputStream in) throws IOException {
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
try {
return readRecetasArray(reader);
} finally {
reader.close();
}
}
public List<Receta> readRecetasArray(JsonReader reader) throws IOException {
List<Receta> recetas = new ArrayList<Receta>();
reader.beginArray();
while (reader.hasNext()) {
recetas.add(readReceta(reader));
}
reader.endArray();
return recetas;
}
public Receta readReceta(JsonReader reader) throws IOException {
String foto = null;
String nombre = null;
String pueblo = null;
String ingredientes = null;
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("foto") && reader.peek() != JsonToken.NULL) {
foto = reader.nextString();
} else if (name.equals("nombre")) {
nombre = reader.nextString();
} else if (name.equals("pueblo")) {
pueblo = reader.nextString();
} else if (name.equals("ingredientes")
&& reader.peek() != JsonToken.NULL) {
ingredientes = reader.nextString();
} else {
reader.skipValue();
}
}
reader.endObject();
return new Receta(foto, nombre, pueblo, ingredientes);
}
}
Thanks so much and sorry but my english is terrible.