简体   繁体   English

JSON对象解析以及如何转义unicode字符

[英]JSON object parsing and how to escape unicode characters

I'm fairly new to javascript and such so I don't know if this will be worded correctly, but I'm trying to parse a JSON object that I read from a database. 我对javascript还是相当陌生,所以我不知道它的措词是否正确,但是我正在尝试解析从数据库读取的JSON对象。 I send the html page the variable from a python script using Django where the variable looks like this: 我使用Django将python脚本中的变量发送给html页面,其中变量如下所示:

{
  "data":{
    "nodes":[
      {
        "id":"n0",
        "label":"Redditor(user_name='awesomeasianguy')"
      },
      ...
    ]
  }
}

Currently, the response looks like: 当前,响应如下所示:

"{u'data': {u'nodes': [{u'id': u'n0', u'label': u"Redditor(user_name='awesomeasianguy')"}, ...

I tried to take out the characters like u&#39 with a replaceAll type statement as seen below. 我试图用replaceAll类型语句取出u&#39之类的字符,如下所示。 This however is not that easy of a solution and it seems like there has got to be a better way to escape those characters. 但是,这并不是那么容易解决的方法,似乎必须有一种更好的方法来逃避这些字符。

var networ_json = JSON.parse("{{ networ_json }}".replace(/u'/g, '"').replace(/'/g, '"').replace(/u"/g, '"').replace(/"/g, '"'));

If there are any suggestions on a method I'm not using or even a tool to use for this, it would be greatly appreciated. 如果有关于我不使用的方法或什至用于此目的的工具的任何建议,将不胜感激。

Use the template filter "|safe" to disable escaping, like, 使用模板过滤器“ | safe”可禁用转义,例如,

var networ_json = JSON.parse("{{ networ_json|safe }}";

Read up on it here: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#safe 在这里阅读: https : //docs.djangoproject.com/en/dev/ref/templates/builtins/#safe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM