andriod读取php的json时出现的bom问题

总是提示项目解析json数据时发现报
10-12 02:36:35.964: W/System.err(323): org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
错误异常,无论更改header的编码还是用纯英文数据都依然提示,搜索一下错误提示,发现有人说是记事本(notepad.exe)保存的php文件保存时带有bom头,解决方案大概有下面几种:
1.继续用记事本保存成ansi就没问题了;
2.换个编辑器重新保存;
3.前面两种情况还是防不胜防,据说在andriod4.0里已有解决方案:

1 public JSONTokener(String in) {
2 // consume an optional byte order mark (BOM) if it exists
3 if (in != null && in.startsWith("\ufeff")) {
4 in = in.substring(1);
5 }
6 this.in = in;
7 }

 

具体用法类似

1  String json = "{"
2          + "  \"query\": \"Pizza\", "
3          + "  \"locations\": [ 94043, 90210 ] "
4          + "}";
5 
6  JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
7  String query = object.getString("query");
8  JSONArray locations = object.getJSONArray("locations");

 

 

posted on 2012-12-31 13:49  jenqz  阅读(316)  评论(0编辑  收藏  举报