是否有一个Jquery函数可以从解析的JSON字符串中获取#ref id值并指向我引用的对象? [英] Is there a Jquery function that can take a #ref id value from a parsed JSON string and point me to the referenced object?

查看:159
本文介绍了是否有一个Jquery函数可以从解析的JSON字符串中获取#ref id值并指向我引用的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整个下午都在寻找答案,我似乎找不到最好的方法来完成我需要的工作。

I have been looking for an answer to this all afternoon and i cant seem to find the best way to accomplish what i need to.

我的JSON字符串(返回来自Web服务)在其中有循环引用(#ref),它指向字符串中的$ id。现在我知道如果使用jquery parseJSON它会创建javascript对象,我可以访问la myObject.MyPropertyName属性。但是,当我到达#ref时,我不确定如何获取ID指向的对象(我假设已经因为反序列化而创建了...

My JSON string (returned from a web service) has circular references in it (#ref) which point to $id in the string. Now i know that if use jquery parseJSON it creates the javascript object and i can access properties a la myObject.MyPropertyName. However, when i get to a #ref, i am unsure how to get the object that ID points to (which i assume is already created as a result of the de-serialization...

我应该迭代对象及其所有子对象,直到我找到它,或者是否有更简单的方法?

Should i be iterating though the object and all its child objects until i find it, or is there an easier way?

$.ajax({
        type: "POST",
        url: "/Task.asmx/GetTask",
        data: "{'id':'" + '27' + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            _Data = $.parseJSON(msg.d ? msg.d : msg);
            _this.Company = _Data[0].t_Program.t_Company;
            _this.Program = _Data[0].t_Program;
            _this.Task = _Data[0];
        },
        complete: function () {

        }
    });

有问题的区域是_Data [0] .t_Program,因为它不会返回一个对象但是r return

The area in question is _Data[0].t_Program because it does not return an object but rather returns

_Data[0].t_Program
 {...}
   $ref: "12"

我不知道获取$ id为12的对象的最佳方法。根据下面的帖子,似乎我应该遍历现有的对象,但我希望有一个jquery函数,这样做...

I dont exactly know the best way to get the object with $id "12". Based on the posts below it seems i should loop through the existing object, but i was hoping there was a jquery function that did that...

非常感谢!

推荐答案

不,jQuery本身无法解析从JSON转换的对象中的循环引用。

No, jQuery is not natively capable of resolving circular references in objects converted from JSON.

我知道的唯一一个库是 Dojo的 dojox.json.ref 模块

The only library for that which I know is Dojo's dojox.json.ref module.

但是,您的服务器应用程序序列化了该JSON不知何故。不要告诉我它使用的解决方案不提供反序列化算法!

But, your server application serializes that JSON somehow. Don't tell me that the solution it uses does not offer a deserialisation algorithm!

这篇关于是否有一个Jquery函数可以从解析的JSON字符串中获取#ref id值并指向我引用的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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