Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

javascript - Is there a Jquery function that can take a #ref id value from a parsed JSON string and point me to the referenced object?

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.

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 () {

        }
    });

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"

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...

Many Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...