NutzCN Logo
问答 form表单提交图片和name为一致的json字符串会出问题
发布于 2540天前 作者 qq_bbe53a68 1411 次浏览 复制 上一个帖子 下一个帖子
标签:

<input type="hidden" class="a11797a820094237bd5dec39cfc80ff9" name="relation" value="{"type":"a11797a820094237bd5dec39cfc80ff9","parentId":65,"parentNumber":"No-1-11"}">
<input type="hidden" class="a11797a820094237bd5dec39cfc80ff9" name="relation" value="{"type":"a11797a820094237bd5dec39cfc80ff9","parentId":65,"parentNumber":"No-1-12"}">
<input type="hidden" class="a11797a820094237bd5dec39cfc80ff9" name="relation" value="{"type":"a11797a820094237bd5dec39cfc80ff9","parentId":65,"parentNumber":"No-1-13"}">
form里有一些name为一致的 value为json的字符串
如果和图片一起上传双引号会带上右斜杠,然后会出现解析不了的问题
带图片是正常的json数组

org.json.JSONException: JSONArray[0] is not a JSONObject.
7 回复
["{\"type\":\"a11797a820094237bd5dec39cfc80ff9\",\"parentId\":31,\"parentNumber\":\"No-1-8\"}", "{}", "{}", "{}", "{}", "{}", "{}"]
[{"type":"a11797a820094237bd5dec39cfc80ff9","parentId":22,"parentNumber":"No-1-6"},{},{},{},{},{},{}]

这是不带图片的 楼上是带图片的

入口方法贴一下

    @At("/addDo")
    @Ok("json")
    @RequiresPermissions("platform.pigeon.base.info.add")
    @AdaptBy(type = WhaleAdaptor.class)
    @SLog(tag = "Pigeon_base_info", msg = "${args[0].id}")
    public Object addDo(@Param("..")Pigeon_base_info pigeonBaseInfo,
                        @Param("pigeonPic")String pigeonPic,
                        @Param("relation")String relation,
                        HttpServletRequest req) {
		try {
            pigeonBaseInfoService.insert(pigeonBaseInfo);
            //添加信鸽图片
            if (StringUtils.isNotBlank(pigeonPic)){
                String[] picList = pigeonPic.split(";");
                for (int i=0;i<picList.length;i++){
                    Pigeon_pic pic=new Pigeon_pic();
                    pic.setPigeonId(pigeonBaseInfo.getId());
                    pic.setPicUrl(picList[i]);
                    pic.setSequence(i);
                    pigeonPicService.insert(pic);
                }
            }
            //添加遗传关系
            if (StringUtils.isNotBlank(relation)){
                if (relation.startsWith("[")){
                    relation.replace("\\","");
                }else {
                relation="["+relation+"]";
                }
                JSONArray array = new JSONArray(relation);
                for (int i=0;i<array.length();i++){
                    JSONObject info = array.getJSONObject(i);
                    if (null!=info&&info.length()!=0){
                        Pigeon_inherit_info pigeonInheritInfo=new Pigeon_inherit_info();
                        pigeonInheritInfo.setPigeonId(pigeonBaseInfo.getId());
                        pigeonInheritInfo.setNumber(pigeonBaseInfo.getRingNumber());
                        //判断是否是子代添加需要特殊处理
                        String type=info.getString("type");
                        if (type.contains("-")){
                            type=type.substring(type.indexOf("-")+1,type.length());
                        }
                        pigeonInheritInfo.setType(type);
                        pigeonInheritInfo.setParentId(info.getInt("parentId"));
                        pigeonInheritInfo.setParentNumber(info.getString("parentNumber"));
                        pigeonInheritInfoService.insert(pigeonInheritInfo);
                    }
                }
            }
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
    }

relation就是作为传过来的json串

用Chrome 的控制台看看服务器返回的是什么

org.json.JSONException: JSONArray[0] is not a JSONObject.
	at org.json.JSONArray.getJSONObject(JSONArray.java:366)
	at cn.wizzer.app.web.modules.controllers.platform.pigeon.PigeonBaseInfoController.addDo(PigeonBaseInfoController.java:147)
	at cn.wizzer.app.web.modules.controllers.platform.pigeon.PigeonBaseInfoController$$NUTZAOP._aop_invoke(PigeonBaseInfoController.java:2)
	at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
	at cn.wizzer.app.web.commons.slog.SLogAopInterceptor.filter(SLogAopInterceptor.java:61)
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
	at cn.wizzer.app.web.modules.controllers.platform.pigeon.PigeonBaseInfoController$$NUTZAOP.addDo(PigeonBaseInfoController.java:1)
	at cn.wizzer.app.web.modules.controllers.platform.pigeon.PigeonBaseInfoController$FM$addDo$00cf94e18e69c715718c8a52bacb57a0.invoke(PigeonBaseInfoController.java)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:31)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:30)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.app.web.commons.processor.XssSqlFilterProcessor.process(XssSqlFilterProcessor.java:35)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.app.web.commons.processor.NutShiroProcessor.process(NutShiroProcessor.java:52)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.app.web.commons.processor.GlobalsSettingProcessor.process(GlobalsSettingProcessor.java:36)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:21)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1621)
	at cn.wizzer.app.web.commons.filter.RouteFilter.doFilter(RouteFilter.java:31)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1621)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1613)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:541)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1592)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1239)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:481)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1561)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1141)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213)
	at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:118)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.Server.handle(Server.java:564)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:320)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
	at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
	at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:122)
	at org.eclipse.jetty.util.thread.strategy.ExecutingExecutionStrategy.invoke(ExecutingExecutionStrategy.java:58)
	at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:201)
	at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:133)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:672)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:590)
	at java.lang.Thread.run(Thread.java:745)

无法解析json

没有文件的时候, 前端用json发过来的吧, 有文件的时候, 走了mulitpart了, 目测是前端问题

要么检查一下前端的逻辑, 要么后端判断一下, 有转义的时候就加一层转换. 那种双引号带转义的json,是一个json字符串再次被转json字符串的结果.

添加回复
请先登陆
回到顶部