• 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
    
  • 相关阅读:
    关于初学者Could not find action or result :No result defined for action com.lyw.action.LoginAction and result success
    tomcat运行时候出现java.net.BindException: Address already in use: JVM_Bind错误解决方法
    关于jQuery中环境配置中的问题
    myeclicps开发web时候复制一个工程改名字后执行出现404错误
    非正常关闭myeclicps后,出现错误Errors occurred during the build.的解决方法
    【软件工程】读《人月神话》有感
    【软件工程】一个学期软件工程课的感想
    【软件工程】“谁是卧底”之NABC分析
    [软件工程] 查找二维数组最大子数组的之和 郭莉莉&李亚文
    【软件工程】软件开发方法综述
  • 原文地址:https://www.cnblogs.com/xfk1999/p/java-hutool-json-parse-problem.html
Copyright © 2020-2023  润新知