Serializing a list of Object using Json.NET

I’m having a object model as below to which i’m binding data retrieving from the database.

public class Student
{
    public virtual string serialNumber
    { set; get;}

    public virtual string studentFname
    { set; get;}

    public virtual string studentLname
    { set; get;}

    public virtual string studentAge
    { set; get;}
}

There will be number of objects as there are multiple student data’s so i should ideally bind the objects to a Collection preferably a LIST<> and my requirement is I should transport this list of object to another code(Android). I’m using Newtonsoft.Json and am trying to convert the object into json. But if I want to pass the list to another code i should convert the entire list as Json. Accordingly what should be my return type and how to serialize it for my code below

using Newtonsoft.Json;
public class GetData
{
    public List<Student> getData()
    {
        List<Student> stData = new List<Student>();
        Student st = new Student();
        string con = "Data Source=MK-001/PC; Initial Catalog=Inventory; Persist Security Info=True; User ID=sa; Password=sqlserver;";
        using (SqlConnection myConnection = new SqlConnection(con))
        {
            string query = "Select * from StudentData";
            SqlCommand Cmd = new SqlCommand(query, myConnection);
            myConnection.Open();
            using (SqlDataReader reader = Cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    st.serialNumber = reader["Serial Number"].ToString();
                    st.studentFname = reader["Student Fname"].ToString();
                    st.studentLname = reader["Student Lname"].ToString();
                    st.studentAge = reader["Student Age"].ToString();

                    //Is this the correct way??
                    var jsonObject = JsonConvert.SerializeObject(set);
                    stData.Add(jsonObject);
                }
                myConnection.Close();
            }
        }
        //Im returning a list here. How to bind this to a json and what should be return type as per the change made?
        return stData;
    }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You should try this:

https://surajdeshpande.com/2013/10/01/json-net-examples/

string jsonString = JsonConvert.SerializeObject(userList);

Deserialize JSON to an Object:

JsonConvert.DeserializeObject<User>(jsonString);

Method 2

You should serialize your List<Student> after you finish loading them from your DB;

string jsonObject = JsonConvert.SerializeObject(stData);

and return the json string from your method:

public string getData()
{  
     // Do stuff
     return jsonString // this is your List<Student> serialized to json
}

And on the other side, deserialize it back to a JSONObject:

JSONArray studentList = new JSONArray(studentJson);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x