52

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();
    }
    ...
}
Community
  • 1
  • 1
user595234
  • 6,007
  • 25
  • 77
  • 101
  • 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. – George Stocker Jul 05 '12 at 13:35
  • @George Stocker He is asking about serializing and you are showing duplicate of deserializing, why? – Govind Malviya Jul 05 '12 at 13:44
  • @GovindKamalaPrakashMalviya Out of the myriad of duplicates, I misread and chose the wrong one. Thanks for catching that. – George Stocker Jul 05 '12 at 13:46

2 Answers2

109

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);
foson
  • 10,037
  • 2
  • 35
  • 53
  • 1
    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 – Lexy Feito Mar 02 '16 at 15:07
  • 2
    @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);` – Kody Jun 07 '17 at 16:11
  • 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 ? – Md Aslam Feb 12 '19 at 17:46
  • @MdAslam please follow the instructions at https://stackoverflow.com/help/how-to-ask to ask a new question – foson Feb 13 '19 at 16:30
41

Use .net inbuilt class JavaScriptSerializer

  JavaScriptSerializer js = new JavaScriptSerializer();
  string json = js.Serialize(obj);
Govind Malviya
  • 13,627
  • 17
  • 68
  • 94