Question

My JSON returns something like this

        {
    data: "Business & Investing (0)",
    attr: {
    id: "91",
    rel: "file",
    dataitem: null,
    datatext: null
    },
    children: [ ]
    },
    {
    data: "Stock Exchange & Money (0)",
    attr: {
    id: "92",
    rel: "file",
    dataitem: null,
    datatext: null
    },
    children: [ ]
    },

i want to change like this "Data" to "title" brfore passing it to view

    {
title: "Business & Investing (0)",
attr: {
id: "91",
rel: "file",
dataitem: null,
datatext: null
},
children: [ ]
},
{
title: "Stock Exchange & Money (0)",
attr: {
id: "92",
rel: "file",
dataitem: null,
datatext: null
},
children: [ ]
},

My Dynatree :

  function CreateCatTree(filter) {

        $("#res_catBar").dynatree({
            postProcess: function (data, dataType) {
                alert("hey");

            },
            initAjax: {

                type: "GET",                
                cache:false,
                url: '@Url.Action("Catalog")',
                data: {
                    filter:filter
                },
            },
            onActivate: function (node) {

                var id = node.data.attr.id;
                event.preventDefault();
                 retrieveCatalog(id);
                     return false;
            }          
        });

    }

No correct solution

OTHER TIPS

You may try this in JS:

if (yourJSON.hasOwnProperty("data")) {
    yourJSON["title"] = yourJSON["data"];
    delete yourJSON["data"];
}

Since you seem to use Dynatree:

you may also implement the postProcess(data, dataType) callback in JavaScript, and modify the Ajax result there, before it is passed to Dynatree.

You should make your own class which has your set of defined properties, and that, then can be serialized through Newtownsoft JSON.

I have a TreeNode.cs

public class TreeNode
{

    [JsonProperty("title")]
    public string title { get; set; }

This is called through a web service as:

 List<TreeNode> myTaskListObj = new List<TreeNode>();

And then Deserialize or serializing the data, as needed:

        //De Serialize
        myTaskListObj = JsonConvert.DeserializeObject<List<TreeNode>>(jsonString);

        //Serialize 
        jsonString = JsonConvert.SerializeObject(myTaskListObj, Formatting.Indented);

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top