Question

I am using a web service that gets data from my active directory and returns an array of objects to my javascript. The problem is the I am the response encapsulated in Object with _type included when I just want to return the value of the object like this

([{ value: "one", label: "Foo" }, { value: "two", label: "Bar" }]). 

But I am receiving this

[Object { __type="Service+NameDTO", value: "one", label: "Foo"}, Object { __type="Service+NameDTO", value:     "two", label: "Bar"}]

Here is my c#

public NameDTO[] GetCompletionList(string prefix)
{
    var nameList = new List<NameDTO>();
    DirectorySearcher search = new DirectorySearcher();
    string strPath = search.SearchRoot.Path;
    DirectoryEntry entry = new DirectoryEntry(strPath);
    search.PageSize = 1000;
    search.Filter = "(&(objectClass=user)(objectCategory=person)(displayName=*" + prefix + "*))";
    foreach (SearchResult sResultSet in search.FindAll())
    {
        if (sResultSet.Properties["mail"].Count > 0)
        {
            var dto = new NameDTO()
            {
                label = (string)sResultSet.Properties["displayName"][0],
                value = (string)sResultSet.Properties["mail"][0],
            };
            nameList.Add(dto);
        }
    }
    NameDTO[] myArray = nameList.ToArray();
    return myArray;
}
public class NameDTO
{
    public string label { get; set; }
    public string value { get; set; }
}

and my javascript

<script type="text/javascript">
    $(document).ready(function () {
        $('#tokenfield').tokenfield({
            autocomplete: {
                source: function (request, response) {
                    $.ajax({
                        url: '<%#ResolveUrl("~/Service1.asmx/GetCompletionList") %>',
                        data: "{ 'prefix': '" + request.term + "'}",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        success: function (data) {
                           response($.map(data.d, function (item) {
                            return data      
                           }))
                        },
                        success: function(response) {
                            $.each(response, function(key, val) {
                                alert(val.id);
                            });
                        },
                        error: function (response) {
                            alert(response.responseText);
                        },
                        failure: function (response) {
                            alert(response.responseText);
                        }
                   });
                },
                search: function () {
                var term = this.value;
                if (term.length < 3) {
                    return false;
                }
                },
                focus: function () {
                // prevent value inserted on focus
                return false;
                },
            },
      });
});
</script>
Was it helpful?

Solution

You probably need some typical JSON builder.

Server-side, you can make dictionaries, arrays, etc and throw everything into a

JsonMapper.ToJson(jsonDict)

call.

This function in my project indirectly calls JsonSerializer(), the Newtonsoft JSON handler. Very useful for both serializing and deserializing the JSON.

I may be misinterpreting your question, though; a bit vague.

http://weblogs.asp.net/scottgu/archive/2007/10/01/tip-trick-building-a-tojson-extension-method-using-net-3-5.aspx is pretty darn relevant here, and very useful.

It came up that you might need this:

var jsonString = @Model; //or however you get your string.
var json = JSON.parse(jsonString);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top