解析由CircularJSON序列化的JSON
Parse JSON serialised by CircularJSON
我的node.js代码中有一个JSON对象,它包含循环引用。为了将这些数据发送到浏览器,我使用了NPM模块circular json来字符串化对象并串行化循环引用:
var CircularJSON = require("circular-json");
app.get("/api/entries", function(req, res){
contentClient.entries({"content_type": "xxxxxxxxxxx"}, function(err, entries){
if (err) throw err;
res.send(CircularJSON.stringify(entries));
});
});
这工作正常,并通过res.send();
发送串行数据现在,在我的前端Angular代码中,我需要使循环引用可用。对象中的一个串行字段在客户端上如下所示:"~0~fields~twitter~1"
我在浏览器中尝试了以下操作:
- 将
circular-json.js
的一个版本复制到我的前端站点 - 链接到我的
index.html
中的脚本,如下所示:<script src="/framework/lib/circular-json.js"></script>
- 将
CircularJson
变量设置为:var CircularJSON = window.CircularJSON;
像这样解析传入的JSON:
$http.get("/api/entries").then(function(res){ console.log(CircularJSON.parse(res.data[0])); });
我得到以下错误:SyntaxError: Unexpected token o
对于一次通话,我认为您可以使用$http,如下
function appendTransform(defaults, transform) {
// We can't guarantee that the default transformation is an array
defaults = angular.isArray(defaults) ? defaults : [defaults];
// Append the new transformation to the defaults
defaults.unshift(transform);
return defaults;
}
$http({
url: '...',
method: 'GET',
transformResponse: appendTransform($http.defaults.transformResponse, function(value) {
return CircularJSON.parse(value);
})
});
或者,对于每个可以定义拦截器的调用,示例如下:https://docs.angularjs.org/api/ng/service/$http
更新:取消移位不会返回数组。修改了代码,现在应该可以工作了。
相关文章:
- 使用JSON序列化图论树的解决方法
- VB.Net JSON 序列化格式
- JSON 序列化 - 如何取消属性值的引号
- 字符串化和解析 JSON(序列化对象结构)和反序列化,将 JSON 字符串转换为对象结构
- DateTime.Min JSON 序列化不正确
- 为JSON序列化以html形式生成数组时出现问题
- javascript中的JSON序列化和php反序列化
- javascript对象的服务器端类型是什么,使用JSON序列化客户端,通过ajax调用传递
- 重写单曲的Newtonsoft.Json序列化
- Javascript 和 Java Date JSON 序列化
- Json序列化器vs .net序列化类的JQuery AJAX结果数据
- 在JavaScript中,是否有一个词来描述可json序列化的对象
- 从JSON序列化的django对象访问字段值
- 如何解决javascript中使用引号的c# Json序列化解析
- Ember.js改变RESTAdapter JSON序列化
- Web2py: json序列化器和javascript有关于python字符串引号的问题
- JSON序列化忽略loadFromJSON之后fabric.js中的自定义属性
- 在json序列化过程中忽略对象属性
- 无法解析json序列化字符串与mongodb对象id以及""
- 重建JSON序列化的对象