0

Am using following code to convert data to Json and return value is: "{\"BillNo\":18}" . But i want to return value for key and value pair Like: {"BillNo":18}

      public String Get()

       {
         var MaxBillNo = db.Billings.Max(b => b.BillNo.Substring(9, 5));
         int i = Int32.Parse(MaxBillNo);
         BillNumber billNumber = new BillNumber();
         billNumber.BillNo = i;
         string json = JsonConvert.SerializeObject(billNumber);
         return json;
       }
Mani
  • 71
  • 1
  • 7
  • Why don't you use [Newtonsoft](http://www.newtonsoft.com/json) – radu-matei Mar 02 '16 at 10:49
  • BillNo is an integer in the BillNumber object, so it will be serialized as an integer without the quotes. – arie Mar 02 '16 at 10:49
  • 4
    Ok so where are you seeing "{\"BillNo\":18}" - Is that from debugging or does it actually print like that? Cos if it's from debugging that's just the quote being escaped, the actual value will be the " by itself. – arie Mar 02 '16 at 11:05
  • No repro. `JsonConvert.SerializeObject(new BillNumber{BillNo=5})` returns `{"BillNo":5}`. You probably got confused by what you see in the debug window – Panagiotis Kanavos Mar 02 '16 at 12:00
  • It looks like you may be double-serializing your JSON. I.e. the returned string is getting serialized to JSON *again* at a higher code level causing the escaping. If you are using [tag:asp.net-web-api] then see [JSON.NET Parser *seems* to be double serializing my objects](https://stackoverflow.com/questions/25559179). Otherwise, please let us know what framework you are using and how you are returning your JSON. – dbc Mar 05 '16 at 17:52

3 Answers3

0

"{\"BillNo\":18}" is valid json string will be parsed in javascript on client side but not {"BillNo":18}

var myJson = "{\"BillNo\":18}";
console.log(JSON.parse(myJson));
asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84
0

Unfortunately it is not possible. It is standard C# string formatting rules. \" is a double quotation mark. See Escape Sequences: https://msdn.microsoft.com/en-us/library/h21280bw.aspx

  • Why is it impossible? In fact, I'll bet the OP confuses the value field in the debug window which shows `\"` for the actual string – Panagiotis Kanavos Mar 02 '16 at 11:48
  • To be able to use double quote in a string you MUST use backslash before. Is is C# specification. – Maksym Voronytskyi Mar 02 '16 at 11:54
  • Only in literals, ie the strings you type in your code. But we aren't talking about that at all. The OP is asking why does the string value contain escape characters? Most likely it doesn't and the OP is checking the value in the debug window. Printed on the console or written to a file, the result would be `{"BillNo":18}` – Panagiotis Kanavos Mar 02 '16 at 11:56
  • We are talking about string. See question. Question is - how to return JSON string result without backslash. – Maksym Voronytskyi Mar 02 '16 at 11:59
0

It is possible in C# but for that you have to pass a list of objects to serialize method. Here is the sample given below, hope so it will be helpful for you.

    SmaccLib smacclib = new SmaccLib();
    smaccDate = smacclib.Date_SaveFormat(date);

    List<EmployeeAbsents> listEmployeeAbsents = _hrManager.EmployeeAbsentManager.SelectEmployeeAbsentsByDate(smaccDate, rowIndex);

    if (listEmployeeAbsents != null && listEmployeeAbsents.Count > 0)
    {
        return JsonConvert.SerializeObject(listEmployeeAbsents);
    }
    return string.Empty;

And you result will be something like this.

"[{"EmployeeAbsentID":"81e930bb-a38e-4b85-ba6c-9cbd6e706872","EmployeeCode":"20","EmployeeName":"Bilal","AbsentDate":"11/09/2016","AbsentTypeCode":"10","AbsentTypeName":"Casual","IsDeductable":true,"Remarks":"re","EntryFrom":0,"RowIndex":4,"OperatingUserID":0,"RecordTimeStamp":"2016-02-19T13:20:44.417"}]"
Muhammad Bilal
  • 125
  • 1
  • 9