Nick Nelson
Nick Nelson

Reputation: 1263

Weird JSON parse Error Android

I am getting a NullPointerException becauase of this LogCat message:

02-17 13:01:10.766: W/System.err(950): org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONArray
02-17 13:01:10.775: W/System.err(950):  at org.json.JSON.typeMismatch(JSON.java:111)

This is what my JSON is outputting:

[{"rated":"1","user_id":""}]

And here is how I am retrieving it:

try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is2,"iso-8859-1"),8);
            sb2 = new StringBuilder();
            sb2.append(reader.readLine() + "\n");

            String line="0";
            while ((line = reader.readLine()) != null) {
                sb2.append(line + "\n");
            }
            is2.close();
            resulttt=sb2.toString();
        }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
        }

        try{
            jArray2 = new JSONArray(resulttt);
            JSONObject json_data=null;

            rateresult = new int[jArray2.length()]; 
            rateuser = new String[jArray2.length()];

            for(int i=0;i<jArray2.length();i++){
                json_data = jArray2.getJSONObject(i);
                rateresult[i]=json_data.getInt("rated");
                rateuser[i]=json_data.getString("user_id");
            }
        }

Any idea what is causing this "mismatch" or weird value in my LogCat? THanks.

Upvotes: 0

Views: 516

Answers (1)

SLaks
SLaks

Reputation: 887275

That's a UTF8 BOM.
You need to read the stream as UTF8, not ISO-8859-1.

Upvotes: 3

Related Questions