2

I have an object of student

Class Student
{
  public Student()
  {
     name = "xyz";
     roll = 20;
     age = 16;
  }
  string name;
  int roll;
  int age;
}

Class Service
{
  Student student = new Student();
  string s = ???
 // what I have to do for getting string like {"name" : "xyz", "roll" : 20, "age" :16}
}

I need the string from the above object.

If i serialize the object I am getting something with escape sequences.

Any help would be greatly appreciable.

2
  • 4
    Add a public override ToString() function there you can return a string an you can format that as you want. Commented Feb 6, 2015 at 6:54
  • 3
    Sounds like you just want JSON. Try JsonConvert.SerializeObject in package Json.NET. Otherwise if you're trying to pretty print something, just use a format string with String.Format. Commented Feb 6, 2015 at 6:57

6 Answers 6

8

Looks like what you really want is to serialize your object to JSON.

This can be easily done with JSON.NET library like this:

Student student = new Student();
var s = JsonConvert.Serialize(student);

If you need this in multiple places you can create an extension method or override the ToString method of Student class:

public class Student
{
     public override string ToString()
     {
          return JsonConvert.Serialize(this);
     }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@satishkumarV it's just that double quotes are escaped when representing the string in C#. If you write this into file, the slashes will be gone
4

You have to serialize your object. For example with the JavaScriptSerializer:

string s = new JavaScriptSerializer().Serialize(student);

Don't forget to add the following using

using System.Web.Script.Serialization;

Comments

3

This is a simplified example, use Vsevolod Goloviznin answer. It is better.

Just use toString();

string s = stuident.toString();

Add an overload to toString() in the class to get pretty print. My personal preferece is to output JSON.

Class Student
{
  public Student()
  {
     name = "xyz";
     roll = 20;
     age = 16;
  }


  public override string toString(){
     return "{name: " + name + ", roll: " + roll + ", age: " + age + "}";
  }

  string name;
  int roll;
  int age;
}

6 Comments

If I added the same it returns Student
@Magic-Mouse why don't you just use String.Format? it makes it more readable, yet easy to handle
@chouaib I assume i was too judgmental or careful. But i figured, if one was not aware of how to override toString. i would make the response as basic as possible, thus just concat strings rather than using potential confusing functions.
No that doesn't diminish the value of your answer at all, I just feel it is more confusing than simple formatting, tastes are not arguable :) . + for the override part: I myself learnt String.Format before I knew what override is :) don't prejudge please, here you go+1
@override is a java annotation!
|
2

Override the ToString() method of your Student class

something like

public override string ToString()
{
    return string.Format("Name: {0}, Roll: {1}, Age: {2}", name, roll, age);
}

Comments

1

The string in your example is JSON. You can use http://www.newtonsoft.com/jsont to serialize: string s = JsonConvert.SerializeObject(student);

Comments

1

You seem to want to serialize it to JSON, check out http://www.newtonsoft.com/json and use it to call string s = JsonConvert.SerializeObject(student);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.