Reputation: 1652
I am calling a web service and receiving this JSON but instantiating the JSONObject class is throwing error, as you can see it contains Persian character (UTF-8) which I don't think could be reason of this problem ,
JSON:
{"teriffs": [
{"name":"برنز","id":"1000","prices":"3;400000-12;600000"},
{"name":"برنز","id":"1000","prices":"3;400000-12;600000"},
{"name":"نقره ای","id":"1002","prices":"3;700000-12;1000000"},
{"name":"نقره ای","id":"1002","prices":"3;700000-12;1000000"}]
}
ERROR:
org.json.JSONException: Value {"teriffs": [{"name":"برنز","id":"1000","prices":"3;400000-12;600000"},{"name":"برنز","id":"1000","prices":"3;400000-12;600000"},{"name":"نقره ای","id":"1002","prices":"3;700000-12;1000000"},{"name":"نقره ای","id":"1002","prices":"3;700000-12;1000000"}]} of type java.lang.String cannot be converted to JSONObject
CODE :
try {
JSONStringer requestMsg = new JSONStringer().object().key("Ticket").value(TempUtil.UID).endObject();
char[] c = CallServiceHelper.getCallService(requestMsg, "/WWWServices.svc/GetTeriffs");
if(c!=null){
JSONObject array = new JSONObject(new String(c));
System.out.println(array.toString());
return array;
} else {
return new JSONObject();
}
} catch (Exception e) {
e.printStackTrace();
}
block
Upvotes: 1
Views: 242
Reputation: 12674
Reason:
Some character cannot be mapped using 'MacRoman" character encoding. Either change the encoding or remove the characters which are not supported by the "MacRoman" character encoding.
Edit
String jsonString = " {\"teriffs\": [{\"name\":\"برنز\",\"id\":\"1000\",\"prices\":\"3;400000-12;600000\"},{\"name\":\"برنز\",\"id\":\"1000\",\"prices\":\"3;400000-12;600000\"},{\"name\":\"نقره ای\",\"id\":\"1002\",\"prices\":\"3;700000-12;1000000\"},{\"name\":\"نقره ای\",\"id\":\"1002\",\"prices\":\"3;700000-12;1000000\"}]}";
try {
String UTF8String = new String(jsonString.getBytes("UTF-8"));
JSONObject object = new JSONObject(UTF8String);
JSONArray array = object.getJSONArray("teriffs");
for(int i=0;i<array.length();i++){
JSONObject jsonObject = array.getJSONObject(i);
Log.d("ID", jsonObject.getString("id"));
Log.d("NAME", jsonObject.getString("name"));
Log.d("PRICES", jsonObject.getString("prices"));
}
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Upvotes: 4