2 qq 29259675 qq_29259675 于 2016.01.28 11:46 提问

将一个List集合转成JSONObject报错:

各位大神,我要异步通过省加载城市,可是转json的时候就报错了
public String getCitys(){

        List<City> citys = cityService.find("from City where province.pid=?", new Object[]{pid});


        JSONObject jo = new JSONObject();
        jo.put("citys", citys);
        HttpServletResponse response = ServletActionContext.getResponse();
        ResponseUtils.renderJson(response, jo.toString());

        return null;
    }

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)
at org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41)
at org.apache.struts2.interceptor.DeprecationInterceptor$$FastClassBySpringCGLIB$$418f35cf.invoke()
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:711)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at org.apache.struts2.interceptor.DeprecationInterceptor$$EnhancerBySpringCGLIB$$52e7eb5c.intercept()
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor$$FastClassBySpringCGLIB$$30837115.invoke()
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:711)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)

5个回答

wojiushiwo945you
wojiushiwo945you   Ds   Rxr 2016.01.28 11:49

list格式的JSON字符串对应的是JSONArray对象的。

qq_29259675
qq_29259675 我是看视频人家使用SpringMVC框架用的一个工具类,我用的是ssh,这根框架有关系吗
接近 2 年之前 回复
qq_29259675
qq_29259675 我用debug运行到jo.put("citys", citys);就报错了
接近 2 年之前 回复
qq_29259675
qq_29259675 然后遍历出来
接近 2 年之前 回复
qq_29259675
qq_29259675 //发送的是JSON public static void renderJson(HttpServletResponse response,String text){ render(response, "application/json;charset=UTF-8", text); }
接近 2 年之前 回复
qq_29259675
qq_29259675 我用一个工具类把他传入前台
接近 2 年之前 回复
wojiushiwo945you
wojiushiwo945you 回复qq_29259675: 你的json转换代码没有问题,你这个异常貌似不是json转换的问题。你为什么返回的是String类型呢?
接近 2 年之前 回复
qq_29259675
qq_29259675 没有用啊,一样的错误,我导的是import org.json.JSONArray; import org.json.JSONObject;包,有什么问题吗
接近 2 年之前 回复
Royal_lr
Royal_lr   Ds   Rxr 2016.01.28 12:36

用gjson转,,基本对象都可以,,

qq_29259675
qq_29259675   2016.01.28 12:55

public String getCitys(){

        List<City> citys = cityService.find("from City where province.pid=?", new Object[]{pid});

        /*JsonConfig jsonConfig = new JsonConfig();

        jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);

        net.sf.json.JSONArray json =net.sf.json.JSONObject.fromArray(citys, jsonConfig);

        net.sf.json.JSONArray jo = net.sf.json.JSONArray.fromObject(citys,jsonConfig);*/

        /*JSONArray jo = new JSONArray(citys);*/

        Gson gson = new Gson(); 
        String jo = gson.toJson(citys); 
        HttpServletResponse response = ServletActionContext.getResponse();
        ResponseUtils.renderJson(response, jo);

        return null;
    }

    我用这个也报错
ya_smile_jy
ya_smile_jy   2016.01.28 14:05

JSONUtil.readParams((String)result.get("data"), JSONUtil.getCollectionType(List.class, OrderGoods.class))

public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {

return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);

}

public static T readParams(String params, Class clz) {
try {
return mapper.readValue(params, clz);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

qq_29312259
qq_29312259   2016.01.28 17:14
    ObjectMapper objectMapper = new ObjectMapper();
    String jsonString = objectMapper.writeValueAsString(obj);
    用这个试一下,obj基本类型都可以,一般传一个map
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!