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/​

 把你要转换的串放在第一红框的位置,然后点下边第二个红框,进行解析,解析过后,就会很清晰的告诉你,是哪里错了。

com.alibaba.fastjson.JSONException: expect ‘:‘ at 224501035, actual “_创建文件

 

第二种情况是,一个非常大的 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变量。这直接调用这个方法就可以了。通过控制台打印的错误信息,就能够知道错在哪里了,看我的控制台报的错误,绿色框,就是错误的位置,具体的行,具体的列。根据这个一眼就能找到错误在哪里了。

com.alibaba.fastjson.JSONException: expect ‘:‘ at 224501035, actual “_json_02

 

 

如果不会把一个大的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();
}
}