1

我正在调用 Web 服务并接收此 JSON,但实例化 JSONObject 类会引发错误,因为您可以看到它包含波斯字符 (UTF-8),我认为这不是这个问题的原因,

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"}]
}

错误:

org.json.JSONException: 值 {"tiffs": [{"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"}]} 类型为 java.lang。字符串无法转换为 JSONObject

代码 :

   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();
            }

堵塞

4

1 回答 1

4

原因:

某些字符无法使用“MacRoman”字符编码进行映射。请更改编码或删除“MacRoman”字符编码不支持的字符。

编辑

 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();
        }
于 2013-07-29T05:31:29.793 回答