53

Possible Duplicate:
Turn C# object into a JSON string in .NET 4

In the Java, I have a code to convert java object to JSON string. How to do the similar in the C# ? which JSON library I should use ?

Thanks.

JAVA code

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ReturnData {
    int total;

    List<ExceptionReport> exceptionReportList;  

    public String getJSon(){
        JSONObject json = new JSONObject(); 

        json.put("totalCount", total);

        JSONArray jsonArray = new JSONArray();
        for(ExceptionReport report : exceptionReportList){
            JSONObject jsonTmp = new JSONObject();
            jsonTmp.put("reportId", report.getReportId());      
            jsonTmp.put("message", report.getMessage());            
            jsonArray.add(jsonTmp);         
        }

        json.put("reports", jsonArray);
        return json.toString();
    }
    ...
}
3
  • You should give this a try in C# and show us the code that you're trying. As written, this question is little more than a google search away from an answer, and doesn't add value above that. Please edit your question to include what you've tried in C#, and what isn't working. Commented Jul 5, 2012 at 13:35
  • @George Stocker He is asking about serializing and you are showing duplicate of deserializing, why? Commented Jul 5, 2012 at 13:44
  • @GovindKamalaPrakashMalviya Out of the myriad of duplicates, I misread and chose the wrong one. Thanks for catching that. Commented Jul 5, 2012 at 13:46

2 Answers 2

112

I have used Newtonsoft JSON.NET (Documentation) It allows you to create a class / object, populate the fields, and serialize as JSON.

public class ReturnData 
{
    public int totalCount { get; set; }
    public List<ExceptionReport> reports { get; set; }  
}

public class ExceptionReport
{
    public int reportId { get; set; }
    public string message { get; set; }  
}


string json = JsonConvert.SerializeObject(myReturnData);
Sign up to request clarification or add additional context in comments.

4 Comments

Hello, this answer works, but is there a way to serialize the object to a javascript format like new {Name = "Myname"} would be serialized to {name: "MyName"}. Thank you
@LexyFeito I see noone has answered your question. Use: var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; settings.Converters.Add(new StringEnumConverter()); var json = JsonConvert.SerializeObject(myReturnData, Formatting.Indented, settings);
I have followed this working fine but if data or object contains more content then it throws Out Of Memory Exception. How to handle it ?
@MdAslam please follow the instructions at stackoverflow.com/help/how-to-ask to ask a new question
41

Use .net inbuilt class JavaScriptSerializer

  JavaScriptSerializer js = new JavaScriptSerializer();
  string json = js.Serialize(obj);

1 Comment

how to handle 'json.put("totalCount", total); ' ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.