json 解析时有特殊符号 报错
地址是http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=96e3cd93b482b82c5acacff05d77fdab&text=android&page=1&per_page=10&format=json&nojsoncallback=1
有特殊符号 " \
到这一行代码就报错了
FlickrResponse flickrResponse = mGson.fromJson(jsonString, FlickrResponse.class);
[解决办法]
里面是Unicode吧,转义一下就行了
[解决办法]
没有jar 环境, js 测试没有问题。
var data = {
"photos": {
"page": 1,
"pages": 47970,
"perpage": 10,
"total": "479693",
"photo": [{
"id": "10468633035",
"owner": "95969298@N04",
"secret": "37591f8bed",
"server": "3730",
"farm": 4,
"title": "Mantano Ebook Reader Premium v2.4.3",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
},
{
"id": "10468549916",
"owner": "91787793@N08",
"secret": "0af7414c50",
"server": "7395",
"farm": 8,
"title": "Invite my pin BB #invite #promote #android #BBM #BBMforAndroid #me #like4like",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
},
{
"id": "10468472424",
"owner": "95969298@N04",
"secret": "30cdd786b8",
"server": "2805",
"farm": 3,
"title": "Kingdom Builder v1.0.1",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
},
{
"id": "10468406154",
"owner": "106116799@N08",
"secret": "974156d399",
"server": "5474",
"farm": 6,
"title": "#1: Dragon Touch\u00ae 7\u201d Blue Dual Core Y88 Google Android 4.1 Tablet PC, Dual Camera, HD 1024\u00d7600, Google Play Pre-load, HDMI, 3D Game Supported (enhanced version of A13) [By TabletExpress]",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
},
{
"id": "10468304474",
"owner": "95969298@N04",
"secret": "aeb66197c4",
"server": "5537",
"farm": 6,
"title": "JUICED CM10.1 &10.2 THEME v1.5",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
},
{
"id": "10468303806",
"owner": "95969298@N04",
"secret": "c0cba08a40",
"server": "3721",
"farm": 4,
"title": "Twin Me! v2.3",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
},
{
"id": "10468149584",
"owner": "95969298@N04",
"secret": "5f638da162",
"server": "3783",
"farm": 4,
"title": "Pool Break Pro v2.3.4",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
},
{
"id": "10468130236",
"owner": "97580276@N08",
"secret": "9e522dd24e",
"server": "5508",
"farm": 6,
"title": "PEMROGRAMAN ANDROID DENGAN APP INVENTOR NO EXPERIENCE REQUIRED",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
},
{
"id": "10468094165",
"owner": "83199578@N06",
"secret": "ab3eac08a5",
"server": "7320",
"farm": 8,
"title": "#old #is #gold #bbm #shit #ios7 #android #iphone #apple #ipod #ipad #bb mobile #socilamedai #fb #twitter #whatsapp #like #keenography",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
},
{
"id": "10467982634",
"owner": "95969298@N04",
"secret": "9065702899",
"server": "3739",
"farm": 4,
"title": "Quake 3 Touch v1.1",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
}]
},
"stat": "ok"
};
"#1: Dragon Touch? 7” Blue Dual Core Y88 Google Android 4.1 Tablet PC, Dual Camera, HD 1024×600, Google Play Pre-load, HDMI, 3D Game Supported (enhanced version of A13) [By TabletExpress]"
public static void main(String[] args) throws Exception {
String s="#1: Dragon Touch\u00ae 7\u201d Blue Dual Core Y88 Google Android 4.1 Tablet PC, Dual Camera, HD 1024\u00d7600, Google Play Pre-load, HDMI, 3D Game Supported (enhanced version of A13) [By TabletExpress]";
System.out.println(decode(s.toCharArray()));;
}
private static String decode(char[] in) throws Exception {
int off = 0;
char c;
char[] out = new char[in.length];
int outLen = 0;
while (off < in.length) {
c = in[off++];
if (c == '\\') {
if (in.length > off) { // 是否有下一个字符
c = in[off++]; // 取出下一个字符
} else {
out[outLen++] = '\\'; // 末字符为'\',返回
break;
}
if (c == 'u') { // 如果是"\\u"
int value = 0;
if (in.length > off + 4) { // 判断"\\u"后边是否有四个字符
boolean isUnicode = true;
for (int i = 0; i < 4; i++) { // 遍历四个字符
c = in[off++];
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + c - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + c - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + c - 'A';
break;
default:
isUnicode = false; // 判断是否为unicode码
}
}
if (isUnicode) { // 是unicode码转换为字符
out[outLen++] = (char) value;
} else { // 不是unicode码把"\\uXXXX"填入返回值
off = off - 4;
out[outLen++] = '\\';
out[outLen++] = 'u';
out[outLen++] = in[off++];
}
} else { // 不够四个字符则把"\\u"放入返回结果并继续
out[outLen++] = '\\';
out[outLen++] = 'u';
continue;
}
} else {
switch (c) { // 判断"\"后边是否接特殊字符,回车,tab一类的
case 't':
c = '\t';
out[outLen++] = c;
break;
case 'r':
c = '\r';
out[outLen++] = c;
break;
case 'n':
c = '\n';
out[outLen++] = c;
break;
case 'f':
c = '\f';
out[outLen++] = c;
break;
default:
out[outLen++] = '\\';
out[outLen++] = c;
break;
}
}
} else {
out[outLen++] = (char) c;
}
}
return new String(out, 0, outLen);
}