将字典数组传递给Javascript

时间:2016-03-02 23:15:58

标签: javascript python django

我在python中有一个字典数组,我想将它传递给我的前端使用。

这就是我pythonArrayObj的样子:

pythonArrayObj =[{"a":1,"b":2,"c":3}, {"a":4,"b":5,"c":6}, {"a":7,"b":8,"c":9}]

但问题是,当我这样做时:

var test = {{ pythonArrayObj }};

由于"报价转换为&quot;而导致错误,如果我在将json.dumps(pythonArrayObj)传递给我的前端之前尝试&#39;,或public Token Login(String username, String password) throws ApiException { Object postBody = null; if (username == null || username.isEmpty()) { throw new ApiException(400, "Missing the required parameters 'username' when calling Login"); } if (password == null || password.isEmpty()) { throw new ApiException(400, "Missing the required parameters 'password' when calling Login"); } // create path and map variables String path = "/token".replaceAll("\\{format\\}", "json"); // query params List<Pair> queryParams = new ArrayList<Pair>(); Map<String, String> headerParams = new HashMap<String, String>(); Map<String, Object> formParams = new HashMap<String, Object>(); formParams.put("username", username); formParams.put("password", password); formParams.put("grant_type", "password"); final String[] accepts = {"application/json", "text/json", "application/xml", "text/xml"}; final String accept = apiClient.selectHeaderAccept(accepts); final String[] contentTypes = {"application/x-www-form-urlencoded"}; final String contentType = apiClient.selectHeaderContentType(contentTypes); String[] authNames = new String[]{}; GenericType<Token> returnType = new GenericType<Token>() { }; return apiClient.invokeAPI(path, "POST", queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType); } 如果我不这样做

有谁知道如何解决这个问题?最近几天我一直坚持下去,非常感谢你的帮助。

谢谢!

1 个答案:

答案 0 :(得分:3)

您可以使用safe filter来阻止引用字符的翻译:

var test = {{ pythonArrayObj|safe }};

将导致

var test = [{'a': 1, 'c': 3, 'b': 2}, {'a': 4, 'c': 6, 'b': 5}, {'a': 7, 'c': 9, 'b': 8}]

在HTML源代码中。