Husain Alhamali
Husain Alhamali

Reputation: 823

Trying to convert List<object> to an array

I have a List of type "Employee" Class object which contains employees details retrieved from the database.

All data are being retrieved to the list successfully, but when I try to convert the list to a JSON array object, I'm getting strange errors.

I've used two methods to accomplish that, but neither of them worked.

Here is the full beind-code:

using System.Web.Script.Serialization;
using System.Web.Script.Services;
using Newtonsoft.Json;

..

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getRecords()
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader reader;
    List<Employee> dataRows = new List<Employee>();

    con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\\Users\\Shebel Ali\\Desktop\\SampleDB.mdf\";Integrated Security=True;Connect Timeout=30");
    con.Open();
    cmd = con.CreateCommand();
    cmd.CommandText = "select * from Employee";

    reader = cmd.ExecuteReader();
    int i = 1;

    for (i=0; i<reader.FieldCount; i++)
    {
        Debug.WriteLine(reader.GetName(i));
    }


    while (reader.Read())
    {
        Employee getEmp = new Employee(reader.GetValue(0).ToString(), reader.GetValue(1).ToString(), reader.GetValue(2).ToString(), reader.GetValue(3).ToString(), reader.GetValue(4).ToString(), reader.GetValue(5).ToString(), reader.GetValue(6).ToString());
        dataRows.Add(getEmp);
    }

    JavaScriptSerializer js = new JavaScriptSerializer();        
    string resp = js.Serialize(dataRows);

    return resp;
}

And here is the Employee class:

[Serializable]
public class Employee: ISerializable
{
    private string empID { get; set; }
    private string fname { get; set; }
    private string lname { get; set; }
    private string address { get; set; }
    private string phone { get; set; }
    private string deptID { get; set; }
    private string email { get; set; }

    public Employee(string empID, string fname, string lname,string address, string phone, string deptID, string email)
    {
        this.empID = empID;
        this.fname = fname;
        this.lname = lname;
        this.phone = phone;
        this.deptID = deptID;
        this.email = email;
        this.address = address;
    }
}

this code returned me an empty JSON array object:

[{},{},{},{},{},{},{},{},{},{},{}]

I've tried also using this:

var string jsonOb = JsonConvert.SerializeObject(dataRows);

but when an error pops up:

<'object' does not contain a definition for 'SerializeObject' and no extension method 'SerializeObject' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)>

I've tried to reinstall the Newtonsoft package several times but I'm getting the same issue.

Any suggestions please?

Upvotes: 0

Views: 170

Answers (1)

Ouarzy
Ouarzy

Reputation: 3043

If Newton.Json returns [{},{},{},{},{},{},{},{},{},{},{}], it means the object are serialized, but it cannot see any property to be serialized, I guess because they are private?

Only public properties or fields can be serialized by default.

Edit after @Sinatr comment: You can also explicitly add JsonPropertyAttribute to private field/property to serialize it (or use custom contract resolver).

Upvotes: 5

Related Questions