实体集合转JSON遇到的问题

    最近在给IOS的写接口,发现跟ITOO写的接口不一样,ITOO是写好方法,然后把dll文件给他们就行了。而这个是他们需要JSON串,写完以后转成JSON串直接给他们,其实对JSON串也不陌生但是也不是很熟悉,没怎么很深的用过。但是其实挺简单的,只要明白JSON串的结构就好。

    但是最近在用的时候就遇到了问题,转的过程中老是失败!!后来经过查找原来是因为我用了hibernate来操作数据库,而json-lib在处理这类结果对象时存在了异常。如下图这个异常。。 

Struts Problem Report

Struts has detectedan unhandled exception:

Messages:

FreeMarker template error!

 

Methodpublic java.lang.Stringorg.apache.commons.lang.exception.NestableRuntimeException.getMessage(int)threw an exception when invoked on net.sf.json.JSONException:java.lang.reflect.InvocationTargetException
The problematic instruction:
----------
==> ${msgs[0][0]} [on line 76, column 25 inorg/apache/struts2/dispatcher/error.ftl]
----------

Javabacktrace forprogrammers:
----------
freemarker.template.TemplateModelException: Method public java.lang.Stringorg.apache.commons.lang.exception.NestableRuntimeException.getMessage(int)threw an exception when invoked on net.sf.json.JSONException:java.lang.reflect.InvocationTargetException
        atfreemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:130)
        at freemarker.ext.beans.SimpleMethodModel.get(SimpleMethodModel.java:138)
        atfreemarker.core.DynamicKeyName.dealWithNumericalKey(DynamicKeyName.java:111)
        at freemarker.core.DynamicKeyName._getAsTemplateModel(DynamicKeyName.java:90)
        atfreemarker.core.Expression.getAsTemplateModel(Expression.java:89)
        at freemarker.core.Expression.getStringValue(Expression.java:93)
        atfreemarker.core.DollarVariable.accept(DollarVariable.java:76)
        at freemarker.core.Environment.visit(Environment.java:209)
        atfreemarker.core.MixedContent.accept(MixedContent.java:92)
        atfreemarker.core.Environment.visit(Environment.java:209)
        atfreemarker.core.IfBlock.accept(IfBlock.java:82)
        atfreemarker.core.Environment.visit(Environment.java:209)
        atfreemarker.core.IfBlock.accept(IfBlock.java:82)
        atfreemarker.core.Environment.visit(Environment.java:209)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        atfreemarker.core.Environment.visit(Environment.java:209)
        atfreemarker.core.Environment.process(Environment.java:189)
        atfreemarker.template.Template.process(Template.java:237)
        atorg.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:748)
        atorg.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:505)
        atorg.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
        atorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
        atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        atorg.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
        atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
        atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
        atorg.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        atorg.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        atjava.lang.Thread.run(Thread.java:662)
Caused by: java.lang.NullPointerException
        atfreemarker.ext.beans.SimpleMemberModel.unwrapArguments(SimpleMemberModel.java:85)
        atfreemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:106)
        ... 37 more

        

    一大串真心的看不懂啊,不过找到关键字一搜就可以明白错误在哪儿呢。但是我却查找了半天。。。

解决方案:

    原先转json串只是这样就可以了。          

JSONArray arvert = JSONArray.fromObject(实体集合);
json =arvert.toString();

    现在需要再加上两句话。 

JsonConfigjsonConfig=new JsonConfig();  
jsonConfig.setExcludes(newString[]{"handler","hibernateLazyInitializer"});  
JSONArrayarvert = JSONArray.fromObject实体集合,jsonConfig);
 json = arvert.toString();

    实体转json也可以用这种办法解决。

    还依稀记得,从刚开始接触这个项目的时候,因为Java没有怎么接触过,遇到问题都是摸不着头脑,后来遇到的问题渐渐的多了,发现越来越不恐惧了,因为知道总有解决办法,而且解决的越来越快,越来越熟练,这就像是一个成长的过程吧。

你可能感兴趣的:(实体集合转JSON遇到的问题)