NutzCN Logo
问答 我要怎么返回JSONObject 对象到前端
发布于 2516天前 作者 smileQQQQQ 2457 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

代码如下,但是我在前端只收到 {}

/**
	 * 图片文字识别
	 * @author smile
	 * @param test
	 * @return
	 */
	@POST
	@Ok("json")
	@At("/lexicalCheck")
	public Object lexicalCheck(@Param("test") String test) {
		try {
			
			JSONObject json_result = getJiuCuo(test);
			//这是json_result:  {"codeDesc":"Success","text_annotate":"睡觉吃饭","code":0,"conf":1,"text":"睡觉吃饭","message":""}
			return json_result;
		} catch (Exception e) {
			System.out.println("error..." + e.getMessage());
			return null;
		}
	}
22 回复

打印一下,看看输出什么

来自炫酷的 NutzCN

2017-12-26 11:19:05,727 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) DEBUG - Found mapping for [POST] path=/cmsapi/cloud/lexicalCheck : LexicalCheck.lexicalCheck(LexicalCheck.java:42)
2017-12-26 11:19:05,728 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'shiroUtil'<class cn.wizzer.framework.util.ShiroUtil>
2017-12-26 11:19:05,729 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - >> Load definition name=shiroUtil
2017-12-26 11:19:05,729 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:169) DEBUG - Found IocObject(shiroUtil) in AnnotationIocLoader(packages=[cn.wizzer, cn.lvche])
2017-12-26 11:19:05,729 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - >> Make...'shiroUtil'<class cn.wizzer.framework.util.ShiroUtil>
2017-12-26 11:19:05,730 cn.wizzer.app.web.commons.plugin.PluginAopConfigure.getInterceptorPairList(PluginAopConfigure.java:33) DEBUG - skip -- cn.wizzer.framework.util.ShiroUtil
2017-12-26 11:19:05,730 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class cn.wizzer.framework.util.ShiroUtil without AOP
2017-12-26 11:19:05,731 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'shiroUtil' to [app]
2017-12-26 11:19:05,731 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'dateUtil'<class cn.wizzer.framework.util.DateUtil>
2017-12-26 11:19:05,731 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - >> Load definition name=dateUtil
2017-12-26 11:19:05,731 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:169) DEBUG - Found IocObject(dateUtil) in AnnotationIocLoader(packages=[cn.wizzer, cn.lvche])
2017-12-26 11:19:05,731 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - >> Make...'dateUtil'<class cn.wizzer.framework.util.DateUtil>
2017-12-26 11:19:05,731 cn.wizzer.app.web.commons.plugin.PluginAopConfigure.getInterceptorPairList(PluginAopConfigure.java:33) DEBUG - skip -- cn.wizzer.framework.util.DateUtil
2017-12-26 11:19:05,731 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class cn.wizzer.framework.util.DateUtil without AOP
2017-12-26 11:19:05,731 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'dateUtil' to [app]
2017-12-26 11:19:05,732 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'stringUtil'<class cn.wizzer.framework.util.StringUtil>
2017-12-26 11:19:05,732 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - >> Load definition name=stringUtil
2017-12-26 11:19:05,734 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:169) DEBUG - Found IocObject(stringUtil) in AnnotationIocLoader(packages=[cn.wizzer, cn.lvche])
2017-12-26 11:19:05,734 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - >> Make...'stringUtil'<class cn.wizzer.framework.util.StringUtil>
2017-12-26 11:19:05,734 cn.wizzer.app.web.commons.plugin.PluginAopConfigure.getInterceptorPairList(PluginAopConfigure.java:33) DEBUG - skip -- cn.wizzer.framework.util.StringUtil
2017-12-26 11:19:05,735 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class cn.wizzer.framework.util.StringUtil without AOP
2017-12-26 11:19:05,735 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'stringUtil' to [app]
2017-12-26 11:19:05,736 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'lexicalCheck'<class cn.lvche.smartC.CmsApi.PublicCloudServer.LexicalCheck>
2017-12-26 11:19:05,736 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - >> Load definition name=lexicalCheck
2017-12-26 11:19:05,737 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:169) DEBUG - Found IocObject(lexicalCheck) in AnnotationIocLoader(packages=[cn.wizzer, cn.lvche])
2017-12-26 11:19:05,737 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - >> Make...'lexicalCheck'<class cn.lvche.smartC.CmsApi.PublicCloudServer.LexicalCheck>
2017-12-26 11:19:05,737 cn.wizzer.app.web.commons.plugin.PluginAopConfigure.getInterceptorPairList(PluginAopConfigure.java:33) DEBUG - skip -- cn.lvche.smartC.CmsApi.PublicCloudServer.LexicalCheck
2017-12-26 11:19:05,737 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class cn.lvche.smartC.CmsApi.PublicCloudServer.LexicalCheck without AOP
2017-12-26 11:19:05,738 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'lexicalCheck' to [app]
2017-12-26 11:19:05,738 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'imageToBase64'<class cn.lvche.qcloud.Utilities.ImageToBase64>
2017-12-26 11:19:05,739 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - >> Load definition name=imageToBase64
2017-12-26 11:19:05,739 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:169) DEBUG - Found IocObject(imageToBase64) in AnnotationIocLoader(packages=[cn.wizzer, cn.lvche])
2017-12-26 11:19:05,739 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - >> Make...'imageToBase64'<class cn.lvche.qcloud.Utilities.ImageToBase64>
2017-12-26 11:19:05,739 cn.wizzer.app.web.commons.plugin.PluginAopConfigure.getInterceptorPairList(PluginAopConfigure.java:33) DEBUG - skip -- cn.lvche.qcloud.Utilities.ImageToBase64
2017-12-26 11:19:05,739 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class cn.lvche.qcloud.Utilities.ImageToBase64 without AOP
2017-12-26 11:19:05,739 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'imageToBase64' to [app]
2017-12-26 11:19:06,158 cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) DEBUG - [POST]URI=/cmsapi/cloud/lexicalCheck 430ms

log.info(Json.toJson(json_result));
log.info(json_result.toString());

2017-12-26 11:30:15,463 cn.lvche.smartC.CmsApi.PublicCloudServer.LexicalCheck.lexicalCheck(LexicalCheck.java:48) INFO  - {
}
2017-12-26 11:30:15,464 cn.lvche.smartC.CmsApi.PublicCloudServer.LexicalCheck.lexicalCheck(LexicalCheck.java:49) INFO  - {"codeDesc":"Success","text_annotate":"睡觉吃饭","code":0,"conf":1,"text":"睡觉吃饭","message":""}
2017-12-26 11:30:15,465 cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) DEBUG - [POST]URI=/cmsapi/cloud/lexicalCheck 306ms

return json_result.toString(); 如果这么返回, "{"codeDesc":"Success","text_annotate":"睡觉吃饭","code":0,"conf":1,"text":"睡觉吃饭","message":""}" 这样的结果,前端很难受

"{\"codeDesc\":\"Success\",\"text_annotate\":\"睡觉吃饭\",\"code\":0,\"conf\":1,\"text\":\"睡觉吃饭\",\"message\":\"\"}"

改成 @Ok("raw")

@Ok("raw:json") // 这样应该更好些

return json_result; 还是一样的 {}

/**
	 * 图片文字识别
	 * @author smile
	 * @param test
	 * @return
	 */
	@POST
	@Ok("raw:json")
	@At("/lexicalCheck")
	public Object lexicalCheck(@Param("test") String test) {
		try {
			
			JSONObject json_result = getJiuCuo(test);
			//这是json_result:  {"codeDesc":"Success","text_annotate":"睡觉吃饭","code":0,"conf":1,"text":"睡觉吃饭","message":""}
			
			log.info(Json.toJson(json_result));
			log.info(json_result.toString());
			return json_result;
			
		} catch (Exception e) {
			System.out.println("error..." + e.getMessage());
			return null;
		}
	}
2017-12-26 11:50:59,173 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - 	 >> Make...'imageToBase64'<class cn.lvche.qcloud.Utilities.ImageToBase64>
2017-12-26 11:50:59,173 cn.wizzer.app.web.commons.plugin.PluginAopConfigure.getInterceptorPairList(PluginAopConfigure.java:33) DEBUG - skip -- cn.lvche.qcloud.Utilities.ImageToBase64
2017-12-26 11:50:59,173 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class cn.lvche.qcloud.Utilities.ImageToBase64 without AOP
2017-12-26 11:50:59,173 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'imageToBase64' to [app] 
2017-12-26 11:50:59,547 cn.lvche.smartC.CmsApi.PublicCloudServer.LexicalCheck.lexicalCheck(LexicalCheck.java:48) INFO  - {
}
2017-12-26 11:50:59,547 cn.lvche.smartC.CmsApi.PublicCloudServer.LexicalCheck.lexicalCheck(LexicalCheck.java:49) INFO  - {"codeDesc":"Success","text_annotate":"睡觉吃饭","code":0,"conf":1,"text":"睡觉吃饭","message":""}
2017-12-26 11:50:59,549 cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) DEBUG - [POST]URI=/cmsapi/cloud/lexicalCheck 387ms

return json_result.toString(); 就是如下返回

"{\"codeDesc\":\"Success\",\"text_annotate\":\"睡觉吃饭\",\"code\":0,\"conf\":1,\"text\":\"睡觉吃饭\",\"message\":\"\"}"

那就是getJiuCuo(test)里面完全写错咯

没道理啊 ,以下是getJiuCuo()方法返回,及日志

try {
			/* call 方法正式向指定的接口名发送请求,并把请求参数params传入,返回即是接口的请求结果。 */
			result = module.call("LexicalCheck", params);
			JSONObject json_result = new JSONObject(result);
			System.out.println("json_result"+json_result);
			return json_result;
		} catch (Exception e) {
			System.out.println("error..." + e.getMessage());
			return null;
		}
2017-12-26 11:59:35,433 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) DEBUG - Found mapping for [POST] path=/cmsapi/cloud/lexicalCheck : LexicalCheck.lexicalCheck(LexicalCheck.java:45)
2017-12-26 11:59:35,434 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'shiroUtil'<class cn.wizzer.framework.util.ShiroUtil>
2017-12-26 11:59:35,434 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'dateUtil'<class cn.wizzer.framework.util.DateUtil>
2017-12-26 11:59:35,434 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'stringUtil'<class cn.wizzer.framework.util.StringUtil>
2017-12-26 11:59:35,434 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'lexicalCheck'<class cn.lvche.smartC.CmsApi.PublicCloudServer.LexicalCheck>
json_result{"codeDesc":"Success","text_annotate":"睡交吃饭","code":0,"conf":1,"text":"睡交吃饭","message":""}
2017-12-26 11:59:36,019 cn.lvche.smartC.CmsApi.PublicCloudServer.LexicalCheck.lexicalCheck(LexicalCheck.java:48) INFO  - {
}
2017-12-26 11:59:36,019 cn.lvche.smartC.CmsApi.PublicCloudServer.LexicalCheck.lexicalCheck(LexicalCheck.java:49) INFO  - {"codeDesc":"Success","text_annotate":"睡交吃饭","code":0,"conf":1,"text":"睡交吃饭","message":""}
2017-12-26 11:59:36,020 cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) DEBUG - [POST]URI=/cmsapi/cloud/lexicalCheck 586ms

百度云纠错API返回值,转成JSONObject 对象,我想把这个对象直接返回到前端

result 是String吗?

那就直接 return result 不要走JSONObject了

百度云返回值有unicode ,前端要单个转或者我要单个转换

{"code":0,"message":"","codeDesc":"Success","text":"\u7761\u89c9\u5403\u996d","text_annotate":"<em>\u7761\u89c9<\/em>\u5403\u996d","conf":2}

用nutz的Json.fromJson嘛

再说, 前端是认这种格式的,不需要转换

哇,return Json.fromJson(json_result); 只要这样就OK了,谢谢大佬

{
  "code": "0",
  "message": "",
  "codeDesc": "Success",
  "text": "睡觉吃饭",
  "text_annotate": "<em>睡觉</em>吃饭",
  "conf": "2"
}

嗯嗯

来自炫酷的 NutzCN

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