0

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.

Jongware
  • 22,200
  • 8
  • 54
  • 100
CarmaZone
  • 21
  • 4
  • and what is your encoding format? Could you please right click on your project -> Properties -> Resource then check your text encoding format – Fatih Santalu Sep 19 '14 at 20:13
  • As the previous commenter suggested. Your code is fine. The source json file should be saved in UTF-8 encoding. The other option is to escape your Unicode characters with \u1234. – Simon Sep 19 '14 at 20:15
  • YES, that was the problem. I don't remembered default codification (cp1252) to text files in eclipse... Thanks so much. – CarmaZone Sep 19 '14 at 21:39
  • Please do not edit your question and add "[Solved]". It invalidates the purpose of Stack Overflow as a "Q&A" site. Add your answer as an *answer*; you can even 'accept' it yourself. – Jongware Sep 28 '14 at 11:35
  • Sorry, I didn't know it, I am new in the community, thanks. – CarmaZone Sep 28 '14 at 17:08

0 Answers0