问题描述
我调用Web服务,并接收该JSON,但实例化的JSONObject类抛出错误,因为你可以看到它包含波斯字符(UTF-8),我不认为可能是原因这个问题,
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:值{teriffs:
[{\"name\":\"برنز\",\"id\":\"1000\",\"prices\":\"3;400000-12;600000\"},{\"name\":\"برنز\",\"id\":\"1000\",\"prices\":\"3;400000-12;600000\"},{\"name\":\"نقره
ای,ID:1002,价格:3; 700000-12; 1000000},{名:نقره
类型1000000}]}; 3; 700000-12ای,ID:1002,价格,
java.lang.String中不能被转换为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();
}
块
推荐答案
原因:
有些字符不能使用的MacRoman字符编码进行映射。
要么改变编码或除去不被的MacRoman字符编码所支持的字符
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.
修改
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();
}
这篇关于JSON解析Android中2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!