I am trying to read data from the file which is in Russian, eclipse gives a log of cracks, first the file is in windows-1251 , everything is fine, I convert it via notepad++ to UTF8 it does not read at all

Error parsing data org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject

but actually I am trying to read the file

 public void addDataToDB() { try { InputStream is = getAssets().open("data.json"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); String str = new String(buffer); parseJson(str); } catch (Exception ex) { Log.d("", "Error, wtf?"); } } 

And it’s impossible to get normal Russian text from a file.

Well, I read Json like this

 public void parseJson(String result) { try { JSONObject json = new JSONObject(result); JSONArray json_data = json.getJSONArray("data"); for (int i = 0; i < json_data.length(); i++) { JSONObject data = json_data.getJSONObject(i); Log.d("", data.getString("name")); ManController.write(getBaseContext(), "'" + data.getString("name") + "'", "'" + data.getString("barcode") + "'"); } } catch (JSONException e) { Log.e("log_tag", "Error parsing data " + e.toString()); } } 
  • one
    and you have a file with json in what encoding is created? - mixailflash
  • windows 1251, when I finish in utf-8 it gives the error that at the beginning of the thread - dajver
  • one
    something I do not see the transcoding, read in the same InputStream encoding is = new ByteArrayInputStream (buf); BufferedReader r = new BufferedReader (new InputStreamReader (is, "win-1251")); - mixailflash
  • Thank! It helped - dajver

1 answer 1

Something I do not see transcoding, read in the same encoding

 InputStream is = new ByteArrayInputStream(buf); BufferedReader r = new BufferedReader( new InputStreamReader(is, "win-1251"));