本文介绍了用反斜杠解析JSON结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前端以以下格式接收json输出(双引号前加反斜杠).我们没有故意放退格键,但是在将输出发送到前端时,Primefaces(JSF)放了退格键.如何解析这个并使用jquery或javascript将其保存在数组中?我们根本不希望primefaces加上反斜杠,如果可以从服务器端(primefaces)做一些事情,请告诉我.我们可以使用javascript中的某些函数删除"\",但是如果我们有意给它加上了反斜杠,则它可能会删除任何反斜杠.例如,如果我要发送双字符串,例如\"foo \"

The front end recieves the json output in the following format (with a backslash before the double quote). We didn't intentionally put the backspace but Primefaces(JSF) does when sending the output to front end. How to parse this and save it in an array using jquery or javascript? We don't want the backslash added by primefaces at all, if there is way we can do from the server side (Primefaces) please let me know. We can remove the "\" using some function in javascript but then it may remove any backslash if we gave it intentionaly. For example if i am sending double string like \"foo\"

{"topicJSON":"{\"topicBody\":[\"Test10\",\"Test22\",\"Test4\",\"Test11\"]}"}

服务器端

jGenerator.writeFieldName("topicBody");             jGenerator.writeStartArray();                  ...............             jGenerator.close();              context = RequestContext.getCurrentInstance();
 context.addCallbackParam("topicJSON", out.toString());

推荐答案

您可以使用jQuery.parseJSON

像这样

var newOne = jQuery.parseJSON(yourJsOldVariable);//yourJsOldVariable contains that string you posted

这篇关于用反斜杠解析JSON结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:10