0

I'm creating objects based on search results I get. I'm then trying to serialize the objects to return a JSON formatted string. I'm trying to accomplish the below scenario. I don't want to hard-code any JSON, I want the JSON to be output just from the object serialization. I'm not sure how to accomplish what I'm looking for. Note I have some user values hard-coded in my example code for simplicity's sake.

My code:

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        getSearchResultsString();
    }

    public void getSearchResultsString()
    {
        string[] userList = { "user1", "user2", "user3" };

        var json = "";

        List<string> users = new List<string>();

        foreach (string user in userList)
        {

            string userName = "jsmith";

            string email = "[email protected]";

            string createdDate = "3/20/2016";

            ADUser aduser = new ADUser(userName, email, createdDate);

            users.Add(new JavaScriptSerializer().Serialize(aduser));
        }
        json = String.Join(", ", users);
        Response.Write(json);
    }

    public class ADUser
    {
        public ADUser(string UserName, string Email, string CreatedDate)
        {
            userName = UserName;
            email = Email;
            createdDate = CreatedDate;
        }

        // Properties.
        public string userName { get; set; }
        public string email { get; set; }
        public string createdDate { get; set; }
    }
}

My current output:

{"userName":"jsmith","email":"[email protected]","createdDate":"3/20/2016"}, {"userName":"jsmith","email":"[email protected]","createdDate":"3/20/2016"}, {"userName":"jsmith","email":"[email protected]","createdDate":"3/20/2016"}

My desired output:

{
    "users": [{
        "userName": "jsmith",
        "email": "[email protected]",
        "createdDate": "3/20/2016"
    }, {
        "userName": "jsmith",
        "email": "[email protected]",
        "createdDate": "3/20/2016"
    }, {
        "userName": "jsmith",
        "email": "[email protected]",
        "createdDate": "3/20/2016"
    }]
}
2
  • 1
    don't make a list of strings, make a list of an object! Commented Mar 20, 2016 at 12:18
  • What do I need to modify/add in my code? Commented Mar 20, 2016 at 12:36

1 Answer 1

2

You can try to revise getSearchResultsString() as blow

public static void getSearchResultsString()
    {
        string[] userList = { "user1", "user2", "user3" };

        var json = "";

        List<ADUser> users = new List<ADUser>();

        foreach (string user in userList)
        {

            string userName = "jsmith";

            string email = "[email protected]";

            string createdDate = "3/20/2016";

            ADUser aduser = new ADUser(userName, email, createdDate);

            users.Add(aduser);
        }
        json = new JavaScriptSerializer().
            Serialize(new { users = users });
        Response.Write(json);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

That was it, thanks! So what does the syntax 'Serialize(new { users = users });' do essentially?
I write like this to simplify the code, it just creates an Anonymous Type, for more information, you can have access to Anonymous Types

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.