Android 关于用JSONObject的new JSONObject(String s)构建含有特殊符号的字符串时报错的解决方案
来源:互联网 发布:二叉树的遍历设计算法 编辑:程序博客网 时间:2024/06/07 03:24
JSONObject(String json)
throws
JSONException {
this
(
new
JSONTokener(json));
}
不难发现JSONObject(Strings)这个构造函数其实并没有做什么工作,只是直接调用了JSONObject的另一个构造函数:
public case '{':
return readObject(); case '[':
return readArray(); case '\'':
case '"':
return nextString((char) c); default:
pos--;
return readLiteral();
}
} private int nextCleanInternal() throwsJSONException {
while (pos < in.length()) {
int c =in.charAt(pos++);
Log.d("pos", "pos="+pos+" "+c+" "+in.length());
switch (c) {
case '\t':
case '':
case '\n':
case '\r':
continue; case '/':
if (pos == in.length()) {
return c;
} char peek = in.charAt(pos);
switch (peek) {
case '*':
// skip a
pos++;
int commentEnd = in.indexOf("*/", pos);
if (commentEnd == -1) {
throw syntaxError("Unterminated comment");
}
JSONObject(JSONTokener readFrom)
throws
JSONException {
Objectobject =readFrom.nextValue();
if
(object
instanceof
JSONObject){
this
.nameValuePairs= ((JSONObject) object).nameValuePairs;
}
else
{
throw
JSON.typeMismatch(object,
"JSONObject"
);
}
}
这个方法也很简单只是调用了JSONToker类的nextValue方法然后把该方法得到的JSONToker的HanshMap对象nameValuePairs赋给JSONObject的nameValuePairs,所以重点在JSONToker的nextValue()方法。
nextValue()的源码:
public Object nextValue() throws JSONException {
int c = nextCleanInternal();
switch (c) {
case -1:
throw syntaxError("End of input");
看nextValue似乎依然找不到原因,在深入研究,其调用的nextCleanInternal()方法: