zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    洛谷mNOIP模拟赛Day2-星空
    洛谷mNOIP模拟赛Day2-将军令
    洛谷mNOIP模拟赛Day2-入阵曲
    洛谷mNOIP模拟赛Day1-斐波那契
    洛谷mNOIP模拟赛Day1-数颜色
    计蒜客NOIP2017提高组模拟赛(五)day1-展览
    HDU1700Points on Cycle(圆心半径)
    HDU1086You can Solve a Geometry Problem too (斜率问题)
    湖南省第十届大学生计算机程序设计竞赛1503: 点到圆弧的距离(atan()函数的应用)
    HDU献给杭电五十周年校庆的礼物 (切蛋糕,线段划分区域)
  • 原文地址:https://www.cnblogs.com/xfk1999/p/java-hutool-json-parse-problem.html
Copyright © 2011-2022 走看看