JSONException

问题

使用HuTool的JSONUtil工具类时遇到JSONUtil.parse()无法解析字符串的问题

@Test
public void test1() {
    String str = "1234";
    System.out.println(JSONUtil.parse(str).toStringPretty());
}

图1 parse方法

parse方法传入一个Object类型对象, 支持String, Array数组, Collection集合, 和Bean对象, 但在解析字符串时抛出异常cn.hutool.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]

cn.hutool.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]

	at cn.hutool.json.JSONTokener.syntaxError(JSONTokener.java:396)
	at cn.hutool.json.JSONObject.init(JSONObject.java:736)
	at cn.hutool.json.JSONObject.init(JSONObject.java:723)
	at cn.hutool.json.JSONObject.init(JSONObject.java:683)
	at cn.hutool.json.JSONObject.<init>(JSONObject.java:202)
	at cn.hutool.json.JSONObject.<init>(JSONObject.java:178)
	at cn.hutool.json.JSONObject.<init>(JSONObject.java:160)
	at cn.hutool.json.JSONObject.<init>(JSONObject.java:143)
	at cn.hutool.json.JSONUtil.parseObj(JSONUtil.java:88)
	at cn.hutool.json.JSONUtil.parse(JSONUtil.java:221)
	at cn.hutool.json.JSONUtil.parse(JSONUtil.java:196)
	at org.casey.basic.hutool.JsonTest.test1(JsonTest.java:22)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

原因

我的失误, 一时忘记了JSON是键值对的格式, 试想, 如果没有key光有value如何构成JSON对象的属性呢

解决

构造一个字典来进行解析, 构造键值对

@Test
public void test2() {
  String str = "1234";
  Dict dict = Dict.create();
  dict.put("data", str);
  System.out.println(JSONUtil.parse(dict).toStringPretty());
}
{
    "data": "1234"
}

Process finished with exit code 0
posted @ 2020-08-02 09:23  FuKai  阅读(3390)  评论(0编辑  收藏  举报