Exception in thread "main" com.alibaba.fastjson.JSONException: expect ':' at 224501035, actual "
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:291)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:559)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:559)
at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1391)
at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1357)
at com.alibaba.fastjson.JSON.parse(JSON.java:170)
at com.alibaba.fastjson.JSON.parse(JSON.java:180)
at com.alibaba.fastjson.JSON.parse(JSON.java:149)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:241)
at com.wshare.tasks.owner.LoadOwner2Es.main(LoadOwner2Es.java:81)
這個錯誤是我在把 JSON格式的String 串轉成JSON 對象的時候報出來的。
沒錯 阿裡的 fastjson,發生解析錯誤的時候根本不告訴你錯在哪裡。這讓很多人一頭霧水。反正是錯了,錯哪裡了我不給你指出來。
錯誤的原因就是,一個錯誤結構的JSON String串。
第一種情況,串比較小
一般情況下, JSON格式的String串,都是比較小的,可以通過線上工具解析
https://www.bejson.com/
把你要轉換的串放在第一紅框的位置,然後點下邊第二個紅框,進行解析,解析過後,就會很清晰的告訴你,是哪裡錯了。
第二種情況是,一個非常大的 JSON格式的 String串
這個時候,上邊的線上工具已經解析不了了。這個時候要借助我下邊寫的工具類來搞定。
/**
* 檢查Json格式的String串,是不是正确的格式,如果不是,錯誤在在哪裡。
* @param json 要判斷的 String串
* @return
*/
public static boolean isValidJSON(final String json) {
boolean valid = false;
try {
final JsonParser parser = new ObjectMapper().getJsonFactory()
.createJsonParser(json);
while (parser.nextToken() != null) {
}
valid = true;
} catch (JsonParseException jpe) {
jpe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return valid;
}
第二種情況下的JSON串,一般情況下都是直接讀取的檔案。如果你能會把你想要判斷的字元串指派給String變量。這直接調用這個方法就可以了。通過控制台列印的錯誤資訊,就能夠知道錯在哪裡了,看我的控制台報的錯誤,綠色框,就是錯誤的位置,具體的行,具體的列。根據這個一眼就能找到錯誤在哪裡了。
如果不會把一個大的JSON類型的String串指派給 String變量
從檔案中讀出來,然後讀成String串。如果不會的話,可以借助我下邊寫的工具類
public static void main(String[] args) {
String typeStr = FileUtil.readFile("C:\\Users\\Guoqi_All_data_Fusion");
}
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import java.io.*;
import java.util.*;
/**
* @Author: xiaochuan
* @Date: 2019/4/6 14:00
* @Description:
*/
public class FileUtil {
private static Timer timer = new Timer();
public static String getProjectPath() {
return new ApplicationHome(FileUtil.class).getSource().getParentFile().toString();
}
/**
* 建立檔案,可選擇是否覆寫舊檔案
* @return file
*/
public static File createFile(String filePath, boolean cover) {
try {
// 保證建立一個新檔案
File file = new File(filePath);
// 如果父目錄不存在,建立父目錄
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
//覆寫舊檔案
if (cover) {
// 如果已存在,删除檔案
if (file.exists()) {
file.delete();
}
}
// 如果檔案不存在,則建立檔案
if (!file.exists()) {
file.createNewFile();
}
return file;
} catch (IOException e) {
throw new RuntimeException("建立檔案失敗,原因" + e.getMessage());
}
}
/**
* 擷取檔案,可配置如果檔案不存在是否建立新檔案
* @param filePath filePath
* @return file
*/
public static File getFile(String filePath, boolean autoCreate) {
File file = new File(filePath);
if (autoCreate) {
// 如果父目錄不存在,建立父目錄
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
// 如果檔案不存在,則建立檔案
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("建立檔案失敗,原因" + e.getMessage());
}
}
} else {
// 如果父目錄不存在,則抛出異常
if (!file.getParentFile().exists()) {
throw new RuntimeException("父目錄不存在");
}
// 如果檔案不存在,則抛出異常
if (!file.exists()) {
throw new RuntimeException("檔案不存在");
}
}
return file;
}
/**
* 定時删除檔案
* @param millisecondValue 時間戳
*/
public static void deleteFileOnTime(String filePath, long millisecondValue) {
File file = new File(filePath);
if (!file.exists()) {
return;
}
timer.schedule(new TimerTask() {
@Override
public void run() {
delteFile(file);
}
//遞歸删除檔案及檔案夾
private void delteFile(File file) {
if (file.isFile()) {
file.delete();
return;
}
File[] filearray = file.listFiles();
if (filearray != null) {
for (File f : filearray) {
if (f.isDirectory()) {
delteFile(f);
} else {
f.delete();
}
}
file.delete();
}
}
}, new Date(System.currentTimeMillis()+millisecondValue));
}
/**
* 将json寫入檔案
* @param filePath 檔案路徑
* @param json 待寫入json
*/
public static void jsonWriteFile(String filePath, JSONObject json, boolean append) {
textWriteFile(getFile(filePath, true), json.toJSONString(), append);
}
/**
* 将text寫入檔案
* @param filePath 檔案路徑
* @param text 待寫入text
*/
public static void textWriteFile(String filePath, String text, boolean append) {
textWriteFile(getFile(filePath, true), text, append);
}
/**
* 将text寫入檔案
* @param file 檔案
* @param text 待寫入text
*/
public static void textWriteFile(File file, String text, boolean append) {
Writer writer = null;
try {
// GB2312 寫cvs時候使用
writer = new OutputStreamWriter(
new FileOutputStream(file, append),
"UTF-8"
);
writer.write(text + "\r\n");
writer.flush();
} catch (IOException e) {
throw new RuntimeException("寫入檔案失敗,原因" + e.getMessage());
} finally {
if (null != writer) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 讀取檔案,如果檔案不存在,則抛出異常
* @param file 檔案路徑
* @return 檔案内容
*/
public static String readFile(File file) {
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try {
if (null == file) {
throw new RuntimeException("file不能為空");
}
if (!file.exists() || !file.isFile()) {
throw new RuntimeException("file不存在");
}
br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line).append("\r\n");
}
} catch (Exception e) {
throw new RuntimeException("讀取檔案失敗,原因" + e.getMessage());
} finally {
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
/**
* 讀取檔案,如果檔案不存在,則抛出異常
* @param file 檔案路徑
* @return 檔案内容
*/
public static List<String> readFileToList(File file) {
List<String> result = new ArrayList<>();
BufferedReader br = null;
try {
if (null == file) {
throw new RuntimeException("file不能為空");
}
if (!file.exists() || !file.isFile()) {
throw new RuntimeException("file不存在");
}
br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
result.add(line.trim());
}
} catch (Exception e) {
throw new RuntimeException("讀取檔案失敗,原因" + e.getMessage());
} finally {
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* file是否包含text
* @param file 檔案
* @param text text
* @return 是否包含true or false
*/
public static boolean containText(File file, String text) {
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try {
if (null == file) {
throw new RuntimeException("file不能為空");
}
if (!file.exists() || !file.isFile()) {
throw new RuntimeException("file不存在");
}
br = new BufferedReader(new FileReader(file));
String line = "";
int size = 0;
while ((line = br.readLine()) != null) {
if (100 == size) {
if (sb.toString().contains(text)) {
return true;
}
sb = new StringBuilder();
size = 0;
}
sb.append(line).append("\r\n");
size++;
}
if (sb.toString().contains(text)) {
return true;
}
return false;
} catch (Exception e) {
throw new RuntimeException("讀取檔案失敗,原因" + e.getMessage());
} finally {
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* file是否包含text
* @param file 檔案
* @param line line
* @return 是否包含true or false
*/
public static boolean containLine(File file, String line) {
BufferedReader br = null;
try {
if (StringUtils.isEmpty(line)) {
throw new RuntimeException("line不能為空");
}
if (null == file) {
throw new RuntimeException("file不能為空");
}
if (!file.exists() || !file.isFile()) {
throw new RuntimeException("file不存在");
}
br = new BufferedReader(new FileReader(file));
String currentLine = "";
while ((currentLine = br.readLine()) != null) {
if (line.equals(currentLine)) {
return true;
}
}
return false;
} catch (Exception e) {
throw new RuntimeException("讀取檔案失敗,原因" + e.getMessage());
} finally {
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 讀取檔案,如果檔案不存在,則抛出異常
* @param filePath 檔案路徑
* @return 檔案内容
*/
public static String readFile(String filePath) {
if (StringUtils.isEmpty(filePath)) {
throw new RuntimeException("filePath不能為空");
}
return readFile(new File(filePath));
}
/**
* 擷取檔案指定行數的資料,用于爬蟲擷取目前要爬的連結
* @param filePath 目标檔案
* @param point 指針
*/
public String getFileLine(String filePath, long point) {
String thisLine = "";
long thisNum = 0;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(FileUtil.getFile(filePath, false)));
while ((thisLine = reader.readLine()) != null) {
if(point == thisNum) {
return thisLine;
}
thisNum++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != reader) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return thisLine;
}
/**
* 擷取檔案總行數(有多少連結)
* @param filePath 檔案路徑
* @return 總行數
*/
public static long getFileCount(String filePath) {
long count = 0;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(FileUtil.getFile(filePath, false)));
while (!StringUtils.isEmpty(reader.readLine())) {
count++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != reader) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return count;
}
/**
* 讀取resource檔案
* @param rec resource
* @return string
*/
public static String readResource(String rec) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
try {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource resource = resolver.getResource(rec);
br = new BufferedReader(new InputStreamReader(resource.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
try {
assert br != null;
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
return sb.toString();
}
}