Asma Gheisari
Asma Gheisari

Reputation: 6264

json serializaing list of objects cause having Object notation for each tuple

I need to have a json with this format:

var projects = [
        { Title: 'Dave Jones', city: 'Phoenix' },
        { Title: 'Jamie Riley', city: 'Atlanta' },
        { Title: 'Heedy Wahlin', city: 'Chandler' },
        { Title: 'Thomas Winter', city: 'Seattle' }
    ];

in web method I serialized list of objects this way:

[WebMethod]
    public string GetUserProfileProjects()
    {
        List<Test> data = new List<Test>() {
            new Test{ Title= "Dave Jones", City= "Phoenix" },
            new Test{ Title= "Jamie Riley", City = "Atlanta" },
            new Test{ Title= "Heedy Wahlin", City= "Chandler" },
            new Test{ Title= "Thomas Winter", City= "Seattle" }
        };
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        return serializer.Serialize(data);
    }

    public class Test
    {
        public string Title { get; set; }
        public string City { get; set; }
    }

but the json result in ajax call is in this format: Json Result

why each item is hooked to an Object ?

Upvotes: 0

Views: 36

Answers (1)

terbubbs
terbubbs

Reputation: 1512

You would want to serialize a IEnumerable<Dictionary<string,string>> to get the desired result.

Test is an object, therefore, each entry is hooked up to that particular object.

As long as you continue to use List<Test> your JSON result will be the same.

Upvotes: 1

Related Questions