我试图编码oauth2 diagram作为json对象中的字符串。但是,当我使用内置的js parseJSON函数“解码”它的输出看起来破碎。任何想法为什么或如何编码它?如何将文本图编码为json?
function show(){
var diagram = ' +----------+\
| Resource |\
| Owner |\
| |\
+----------+\
^\
|\
(B)\
+----|-----+ Client Identifier +---------------+\
| -+----(A)-- & Redirection URI --->| |\
| User- | | Authorization |\
| Agent -|----(B)-- User authenticates -->| Server |\
| | | |\
| |<---(C)--- Redirection URI ----<| |\
| | with Access Token +---------------+\
| | in Fragment\
| | +---------------+\
| |----(D)--- Redirection URI ---->| Web-Hosted |\
| | without Fragment | Client |\
| | | Resource |\
| (F) |<---(E)------- Script ---------<| |\
| | +---------------+\
+-|--------+\
| |\
(A) (G) Access Token\
| |\
^ v\
+---------+\
| |\
| Client |\
| |\
+---------+\
'
x = JSON.stringify(diagram)
document.write(JSON.parse(x));
}
结果
+----------+ | Resource | | Owner | | | +----------+^| (B) +----|-----+ Client Identifier +---------------+ | -+----(A)-- & Redirection URI --->| | | User- | | Authorization | | Agent -|----(B)-- User authenticates -->| Server | | | | | | |<---(C)--- Redirection URI ----<| | | | with Access Token +---------------+ | | in Fragment | | +---------------+ | |----(D)--- Redirection URI ---->| Web-Hosted | | | without Fragment | Client | | | | Resource | | (F) |<---(E)------- Script ---------<| | | | +---------------+ +-|--------+ | | (A) (G) Access Token | |^v +---------+ | | | Client | | | +---------+
UPDATE:这不工作,要么
// Code goes here
function show(){
var diagram = ' +----------+\n\
| Resource |\n\
| Owner |\n\
| |\n\
+----------+\n\
^\n\
|\n\
(B)\n\
+----|-----+ Client Identifier +---------------+\n\
| -+----(A)-- & Redirection URI --->| |\n\
| User- | | Authorization |\n\
| Agent -|----(B)-- User authenticates -->| Server |\n\
| | | |\n\
| |<---(C)--- Redirection URI ----<| |\n\
| | with Access Token +---------------+\n\
| | in Fragment\n\
| | +---------------+\n\
| |----(D)--- Redirection URI ---->| Web-Hosted |\n\
| | without Fragment | Client |\n\
| | | Resource |\n\
| (F) |<---(E)------- Script ---------<| |\n\
| | +---------------+\n\
+-|--------+\n\
| |\n\
(A) (G) Access Token\n\
| |\n\
^ v\n\
+---------+\n\
| |\n\
| Client |\n\
| |\n\
+---------+\n\
'
x = JSON.stringify(diagram)
document.write("<p>"+ JSON.parse(x) +"</p>");
}
如果你输出到HTML,您将需要'空白:预包装'或'
'标签 – Eric
@tags预包装似乎没有太大的帮助http://plnkr.co/edit/P6L0wGv5fMz7ZkgdnG7w?p=preview –
这是因为'document.write'是可怕的并完全抹去文档的其余部分(如包含样式表的链接标记) – Eric