是否有JSON.Net的javascript序列化程序? [英] Is there a javascript serializer for JSON.Net?

查看:84
本文介绍了是否有JSON.Net的javascript序列化程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Newtonsoft JSON.Net来反序列化一个对象 PreserveReferencesHandling 。 jQuery不支持基于$ ref和$ id语法重新链接引用JSON.Net使用(我不知道jQuery是否支持任何容量的此功能)。

I am using Newtonsoft JSON.Net to deserialize an object with PreserveReferencesHandling enabled. jQuery does not support relinking references based on the $ref and $id syntax JSON.Net uses (I don't know if jQuery supports this functionality in any capacity).

我尝试使用Douglas Crockford的 cycle.js ,但这似乎不适用于我的对象,返回的对象与传入的对象相同。

I tried using Douglas Crockford's cycle.js but that does not seem to work with my objects, the returned object is identical to the object which got passed in.

I我对JSON.Net不是很熟悉,但我似乎找不到任何可以序列化(或解析)JSON的.NET组件输出的javascript库。

I am not incredibly familiar with JSON.Net, but I cannot seem to find any javascript libraries which would serialize (or parse) the JSON their .NET component outputs.

怎么能我完成了对对象引用的重新组合?

How can I accomplish putting back together object references?

推荐答案

我一直在寻找这个问题的解决方案,并最终攻击道格拉斯Crockford的 JSON.retrocycle 功能。他的函数不适用于$ ref = 某些数字,但它会查找类似xpath的内容。

I was looking for a solution to this problem as well, and ended up hacking Douglas Crockford's JSON.retrocycle function. His function does not work for the $ref=some number, but it looks for something like an xpath.

这是我的快速和肮脏版本 - 不要按原样使用它 - 我没有进行任何清理,它可能应该是一个插件,但是它可以完成工作并且足够好用于开始:

This is my quick and dirty version - don't use this as is - I'm not doing any cleanup, and it probably should be a plugin, but it does the job and is good enough to get going:

function retrocycle(o) {
var self = this;
self.identifiers = [];
self.refs = [];

self.rez = function (value) {

    // The rez function walks recursively through the object looking for $ref
    // properties. When it finds one that has a value that is a path, then it
    // replaces the $ref object with a reference to the value that is found by
    // the path.

    var i, item, name, path;

    if (value && typeof value === 'object') {
        if (Object.prototype.toString.apply(value) === '[object Array]') {
            for (i = 0; i < value.length; i += 1) {
                item = value[i];
                if (item && typeof item === 'object') {
                    path = item.$ref;
                    if (typeof path === 'string' && path != null) {
                        //self.refs[parseInt(path)] = {};

                        value[i] = self.identifiers[parseInt(path)]
                    } else {
                        self.identifiers[parseInt(item.$id)] = item;
                        self.rez(item);
                    }
                }
            }
        } else {
            for (name in value) {
                if (typeof value[name] === 'object') {
                    item = value[name];
                    if (item) {
                        path = item.$ref;
                        if (typeof path === 'string' && path != null) {
                            //self.refs[parseInt(path)] = {};

                            value[name] = self.identifiers[parseInt(path)]
                        } else {
                            self.identifiers[parseInt(item.$id)] = item;
                            self.rez(item);
                        }
                    }
                }
            }
        }
    }

};
self.rez(o);
self.identifiers = [];
}

像这样使用:

    $.post("url/function", { ID: params.ID }, function (data) {

        retrocycle(data)

        // data references should be fixed up now

    }, "json");

这篇关于是否有JSON.Net的javascript序列化程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆