Exception in thread "main" org.json.JSONException: Unterminated string at charac

freedomroad 2016-09-23 02:18:15
这是我的hadoop源代码,不知道为什么总出现这个错误,我试过用scaner.next().replaceall().把换行替换但还是出现同样的问题,求助
我的源文件格式是这样的
{"iname":"曹阿山","regDate":"2013年01月22日","id":2253}
四川
{"iname":"唐博湖","regDate":"2013年01月14日","id":2281}


package com.wanfin.hadooptest;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import org.json.JSONException;
import org.json.JSONObject;
public class Test {
public static void main(String[] args) throws JSONException {

String fullFileName = "E:/data/court_data.txt";
File file = new File(fullFileName);
Scanner scanner = null;
// StringBuilder buffer = new StringBuilder();.replace(/[\n]/ig,'')
try {
scanner = new Scanner(file, "utf-8");
while (scanner.hasNextLine()) {
JSONObject jsonObject = new JSONObject(scanner.next());
String name = jsonObject.getString("areaName");
System.out.println(name);
System.out.println(jsonObject.toString());

}
} catch (FileNotFoundException e) {

} finally {
if (scanner != null) {
scanner.close();
}
}

}
}


出现错误如下:


"iname":"唐付长","regDate":"2013年01月14日","id":2281}
Exception in thread "main" org.json.JSONException: Unterminated string at character 261
at org.json.JSONTokener.syntaxError(JSONTokener.java:410)
at org.json.JSONTokener.nextString(JSONTokener.java:244)
at org.json.JSONTokener.nextValue(JSONTokener.java:341)
at org.json.JSONObject.<init>(JSONObject.java:205)
at org.json.JSONObject.<init>(JSONObject.java:402)
at com.wanfin.hadooptest.Test.main(Test.java:25)
...全文
785 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
迟到_啦 2017-10-26
  • 打赏
  • 举报
回复
怎么解决的呢 我也遇到这个问题了
freedomroad 2016-10-17
  • 打赏
  • 举报
回复
自己,已经解决了,谢谢各位能回答我的问题
freedomroad 2016-09-29
  • 打赏
  • 举报
回复
引用 3 楼 hemowolf 的回复:
被解析的 json 数据有问题 把 json 放到一个 html 文件里,用 js 检查一下
用json在线检测工具检测每一个记录都没有问题,就是map()读取数据的出现json格式的问题
freedomroad 2016-09-29
  • 打赏
  • 举报
回复
谢谢,大神,就是这个文件大概有2个多G,会有很多条数据都是出现json格式不正确的问题,然后我用消除空格换行等方式之后还是有很多json格式问题,代码如下: protected void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException { String keys=null; Text text = new Text(); IntWritable intw = new IntWritable(); int count=0; try { Pattern p = Pattern.compile("\\s*|\t|\r|\n"); Matcher m = p.matcher(value.toString()); String str = m.replaceAll(""); JSONObject jsonObject = new JSONObject(str); keys=jsonObject.getString("areaName"); } catch (JSONException e) { e.printStackTrace(); } count++; text.set(keys); intw.set(count); context.write(text, intw); } 现在就是想如何不用etl清洗数据,就能把数据格式化统一呢?
小灰狼 2016-09-23
  • 打赏
  • 举报
回复
被解析的 json 数据有问题 把 json 放到一个 html 文件里,用 js 检查一下
家里敷泥呀 2016-09-23
  • 打赏
  • 举报
回复
字符串不符合json语法,在第261个字符处。 把字符串打印出来,然后检查一下就行了。
fondOfJava 2016-09-23
  • 打赏
  • 举报
回复

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧