w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
JavaScript/GSON: Access JSON references dynamically over object graph (circular references)

!! UPDATE, BETTER SOLUTION !!

If you can switch your library, just use FlexJson >>> http://flexjson.sourceforge.net/.


I solved my problem with an own JSON parser:

"ref" is "0x[n]" in the original GraphAdapterBuilder

Source:

$.ajax({
    type: "POST",
    url: "controller/ajaxmethod.htm",
    data: { "var1": var1, "var2":var2},
    success: function(response){
    var jsonObject = parseGraphGSON(response, 0);
            ...
    },
    error: function(e){
          alert('Error: ' + e.status);
    }
});


function parseGraphGSON(gsonResponse, recursionLevel) {
    var maxRecursionDepth = 2;
    var jsonObject = JSON.parse(gsonResponse, function(key, value) {
        if (typeof value === 'string') {
            if (value.indexOf("ref") == 0) {
                if (recursionLevel < maxRecursionDepth) {
                    return parseGraphGSON(gsonResponse, recursionLevel +
1)[value];
                } else {
                    return JSON.parse(gsonResponse)[value];
                    }
            }
        }
        return value;
    });
    return jsonObject;
}




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.