[alibaba/fastjson]使用JSON.parseObject(stanResult)报太长无法解析,有什么好的解决方法?

2024-05-22 803 views
6

具体报错如下:

Exception in thread "main" com.alibaba.fastjson.JSONException: syntax error, pos 1, line 1, column 2Request is too long to be handled by server: 12984611 characters. Max length is 100000 characters.

回答

9

1298万?

8

1298万?

是的,保存下来这个json文件有13.39MB。这是从服务器返回的json就这么大,然后fastjson解析不了,还有什么比较好的解析大文本json库么,或者fastjson有什么解析大文本大方法?

0

13M怎么可能解析不了呢

6

13M怎么可能解析不了呢

那这个错误不是数据太大的原因么。如果需要这个json数据,我把数据发给你测试一下

7

https://github.com/alibaba/fastjson/wiki/Stream-api

首先感谢你提供的解决思路,我尝试按照里面提供的示例来进行编写,可以解析但是出现了一个错误

我的json数据结构大概是这样的:

{
    "sentences":[
    {...},
    {...},
    .....
    {...}
]
}

这是我的代码:

import com.alibaba.fastjson.JSONReader

object testFastJson {
  def main(args: Array[String]): Unit = {
    val reader = new JSONReader(new FileReader("./stenResult.json"))
    reader.startObject()
    while (reader.hasNext) {
      val key: String =reader.readString()
      println(key)
    }
    reader.endObject()
    reader.close()
  }
}

我运行了上面的代码,得到了一个key,也得到了一个错误 下面是输出的信息:

sentences
Exception in thread "main" com.alibaba.fastjson.JSONException: illegal state : 1002
    at com.alibaba.fastjson.JSONReader.hasNext(JSONReader.java:160)
    at com.bignuo.testFastJson$.main(testFastJson.scala:16)
    at com.bignuo.testFastJson.main(testFastJson.scala)
2

https://github.com/alibaba/fastjson/wiki/Stream-api

首先感谢你提供的解决思路,我尝试按照里面提供的示例来进行编写,可以解析但是出现了一个错误

我的json数据结构大概是这样的:

{
    "sentences":[
    {...},
    {...},
    .....
    {...}
]
}

这是我的代码:

import com.alibaba.fastjson.JSONReader

object testFastJson {
  def main(args: Array[String]): Unit = {
    val reader = new JSONReader(new FileReader("./stenResult.json"))
    reader.startObject()
    while (reader.hasNext) {
      val key: String =reader.readString()
      println(key)
    }
    reader.endObject()
    reader.close()
  }
}

我运行了上面的代码,得到了一个key,也得到了一个错误 下面是输出的信息:

sentences
Exception in thread "main" com.alibaba.fastjson.JSONException: illegal state : 1002
  at com.alibaba.fastjson.JSONReader.hasNext(JSONReader.java:160)
  at com.bignuo.testFastJson$.main(testFastJson.scala:16)
  at com.bignuo.testFastJson.main(testFastJson.scala)

可以提供一个能复现的demo吗(包含数据),咋们一起看看

1

https://github.com/alibaba/fastjson/wiki/Stream-api

首先感谢你提供的解决思路,我尝试按照里面提供的示例来进行编写,可以解析但是出现了一个错误 我的json数据结构大概是这样的:

{
    "sentences":[
    {...},
    {...},
    .....
    {...}
]
}

这是我的代码:

import com.alibaba.fastjson.JSONReader

object testFastJson {
  def main(args: Array[String]): Unit = {
    val reader = new JSONReader(new FileReader("./stenResult.json"))
    reader.startObject()
    while (reader.hasNext) {
      val key: String =reader.readString()
      println(key)
    }
    reader.endObject()
    reader.close()
  }
}

我运行了上面的代码,得到了一个key,也得到了一个错误 下面是输出的信息:

sentences
Exception in thread "main" com.alibaba.fastjson.JSONException: illegal state : 1002
    at com.alibaba.fastjson.JSONReader.hasNext(JSONReader.java:160)
    at com.bignuo.testFastJson$.main(testFastJson.scala:16)
    at com.bignuo.testFastJson.main(testFastJson.scala)

可以提供一个能复现的demo吗(包含数据),咋们一起看看

我测试的代码和json已经发送到你邮箱了

7

https://github.com/alibaba/fastjson/wiki/Stream-api

首先感谢你提供的解决思路,我尝试按照里面提供的示例来进行编写,可以解析但是出现了一个错误 我的json数据结构大概是这样的:

{
    "sentences":[
    {...},
    {...},
    .....
    {...}
]
}

这是我的代码:

import com.alibaba.fastjson.JSONReader

object testFastJson {
  def main(args: Array[String]): Unit = {
    val reader = new JSONReader(new FileReader("./stenResult.json"))
    reader.startObject()
    while (reader.hasNext) {
      val key: String =reader.readString()
      println(key)
    }
    reader.endObject()
    reader.close()
  }
}

我运行了上面的代码,得到了一个key,也得到了一个错误 下面是输出的信息:

sentences
Exception in thread "main" com.alibaba.fastjson.JSONException: illegal state : 1002
  at com.alibaba.fastjson.JSONReader.hasNext(JSONReader.java:160)
  at com.bignuo.testFastJson$.main(testFastJson.scala:16)
  at com.bignuo.testFastJson.main(testFastJson.scala)

可以提供一个能复现的demo吗(包含数据),咋们一起看看

我测试的代码和json已经发送到你邮箱了

你的数据我用相应版本fastjson解析没问题,你用Java试试,scale不知道怎么运行 image

6

https://github.com/alibaba/fastjson/wiki/Stream-api

首先感谢你提供的解决思路,我尝试按照里面提供的示例来进行编写,可以解析但是出现了一个错误 我的json数据结构大概是这样的:

{
    "sentences":[
    {...},
    {...},
    .....
    {...}
]
}

这是我的代码:

import com.alibaba.fastjson.JSONReader

object testFastJson {
  def main(args: Array[String]): Unit = {
    val reader = new JSONReader(new FileReader("./stenResult.json"))
    reader.startObject()
    while (reader.hasNext) {
      val key: String =reader.readString()
      println(key)
    }
    reader.endObject()
    reader.close()
  }
}

我运行了上面的代码,得到了一个key,也得到了一个错误 下面是输出的信息:

sentences
Exception in thread "main" com.alibaba.fastjson.JSONException: illegal state : 1002
    at com.alibaba.fastjson.JSONReader.hasNext(JSONReader.java:160)
    at com.bignuo.testFastJson$.main(testFastJson.scala:16)
    at com.bignuo.testFastJson.main(testFastJson.scala)

可以提供一个能复现的demo吗(包含数据),咋们一起看看

我测试的代码和json已经发送到你邮箱了

你的数据我用相应版本fastjson解析没问题,你用Java试试,scale不知道怎么运行 image

经过测试原来的parseObject好像又可以了,没有报长度的错误了。但是Stream API里面提供的方法,用JSONReader来进行解析还是有错误,同样是这一份数据,你可以测试一下

5

https://github.com/alibaba/fastjson/wiki/Stream-api

首先感谢你提供的解决思路,我尝试按照里面提供的示例来进行编写,可以解析但是出现了一个错误 我的json数据结构大概是这样的:

{
    "sentences":[
    {...},
    {...},
    .....
    {...}
]
}

这是我的代码:

import com.alibaba.fastjson.JSONReader

object testFastJson {
  def main(args: Array[String]): Unit = {
    val reader = new JSONReader(new FileReader("./stenResult.json"))
    reader.startObject()
    while (reader.hasNext) {
      val key: String =reader.readString()
      println(key)
    }
    reader.endObject()
    reader.close()
  }
}

我运行了上面的代码,得到了一个key,也得到了一个错误 下面是输出的信息:

sentences
Exception in thread "main" com.alibaba.fastjson.JSONException: illegal state : 1002
  at com.alibaba.fastjson.JSONReader.hasNext(JSONReader.java:160)
  at com.bignuo.testFastJson$.main(testFastJson.scala:16)
  at com.bignuo.testFastJson.main(testFastJson.scala)

可以提供一个能复现的demo吗(包含数据),咋们一起看看

我测试的代码和json已经发送到你邮箱了

你的数据我用相应版本fastjson解析没问题,你用Java试试,scale不知道怎么运行 image

经过测试原来的parseObject好像又可以了,没有报长度的错误了。但是Stream API里面提供的方法,用JSONReader来进行解析还是有错误,同样是这一份数据,你可以测试一下

确实会报错