如何在Javascript中解码JSON?
问题描述:
我有一个用Python编码的JSON对象.我不知道我是否正确地进行编码.
I have a JSON object which I encoded in Python. I don't know if I am doing the encoding right or not.
proc = subprocess.Popen(['sshpass',
'-p',
password,
'rsync',
'-avz',
'--info=progress2',
source12,
destination],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE).communicate()[0]
progress = json.dumps(proc)
return HttpResponse(progress, mimetype="application/json")
我想使用JAVASCRIPT中的对象"progress"在Django模板中显示进度栏.怎么做?谢谢
I want to use the object 'progress' in the JAVASCRIPT to show the progress bar in the Django template. How it can be done? Thanks
我正在尝试在Django模板中实现它,
I am trying to implement it in a Django template like this:
<script type="text/javascript" language="javascript">
function popUp() {
var jsProgress = JSON.parse(progress)
document.write(jsProgress)
}
</script>
但这什么也没显示.
答
You can decode any string(don't care about mime type) with JSON.parse("string") https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/parse
因此只需将服务器的响应传递给该方法,如下所示:
So just pass response from server to this method like that:
var jsObject = JSON.parse(server.response);