JSON 파싱시, Unexpected character (t) at position 에러

    JSON 파싱 에러


      Error 메세지


    Unexpected character (i) at position 14.

    at org.json.simple.parser.Yylex.yylex(Unknown Source)

    at org.json.simple.parser.JSONParser.nextToken(Unknown Source)

    at org.json.simple.parser.JSONParser.parse(Unknown Source)

    at org.json.simple.parser.JSONParser.parse(Unknown Source)

    at co.kr.saramin.friday.morph.module.MorphemeAnalyzer.readTalk(MorphemeAnalyzer.java:116)

    at co.kr.saramin.friday.morph.module.MorphemeAnalyzer.<init>(MorphemeAnalyzer.java:41)

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)

    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:76)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)

    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)

    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)

    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)

    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)

    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)

    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)

    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)

    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)

    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)

    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)

    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)

    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)

    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)

    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)

    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)

    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)

    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)

    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)

    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)

    at javax.servlet.GenericServlet.init(GenericServlet.java:158)

    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1269)

    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1182)

    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1072)

    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5368)

    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5660)

    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)

    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)

    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)

    at java.util.concurrent.FutureTask.run(FutureTask.java:266)

    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

    at java.lang.Thread.run(Thread.java:745)


    위 에러는 JSON 데이터가 형식을 지키지 않았을 경우 발생을 하는데 이 에러가 발생했던 내 JSON  데이터는 아래와 같다


      Error 원인


    {"items":[

    {idx: 1, "pattern": ["안녕"], "rule": ["그래 나도 안녕","그래 안녕이다"],"type": "talk"}

    ,{idx: 2, "pattern": ["잘가"], "rule": ["그래 너도 잘가"],"type": "talk"}

    ]}


    보면, 다른 필드명에 모두 더블 쿼테이션(쌍따옴표)가 있지만, idx 부분에만 더블쿼테이션이 없는데, 이로 인해서 에러가 발생한 것이다.

    문제의 해결 방법은 2가지가 존재하는데 



      해결 방안


    첫째, 더블쿼테이션이 없어도 무시하고 데이터 읽기(Jackson 라이브러리일 경우에만 허용되는 듯, google simple json은 안되는 것 같다


    JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES



    둘째, 모든 필드명에 더블쿼테이션 적용하기


    {"items":[

    {"idx": 1, "pattern": ["안녕"], "rule": ["그래 나도 안녕","그래 안녕이다"],"type": "talk"}

    ,{"idx": 2, "pattern": ["잘가"], "rule": ["그래 너도 잘가"],"type": "talk"}

    ]}


    반응형

    댓글

    Designed by JB FACTORY