0

I'm trying to construct some paging for a WebApi I'm writing.
I need to return back the total of all records along with the request display amount in JSON.

EDIT

Ok, so taking your adivce onbaord, i serialize my object, but i stil get the black slashes.

"[
    {
        \"RowNumber\": 1,
        \"TotalRows\": 10,
        \"TotalDisplayRows\": 10
        \"Sender\": \"MNBLGB2L\"
    },
    {
        \"RowNumber\": 2,
        \"TotalRows\": 10,
        \"TotalDisplayRows\": 10
    },
    {
        \"RowNumber\": 3,
        \"TotalRows\": 10,
        \"TotalDisplayRows\": 10

    },
    {
        \"RowNumber\": 4,
        \"TotalRows\": 10,
        \"TotalDisplayRows\": 10

    },
    {
        \"RowNumber\": 5,
        \"TotalRows\": 10,
        \"TotalDisplayRows\": 10
           }
]"

Code that creates this is :

        List<Summary> results = MtFacade.GetSummary(query);

            string jsonData = JsonConvert.SerializeObject(results);

            return jsonData;

the Summary class is :

[Serializable]
    public class Summary
    {
        public int RowNumber { get; set; }
        public int TotalRows { get; set; }
        public int TotalDisplayRows { get; set; }
    }

So, using both Javascript serializer and JSON produces the back slashes:

screen grab within VS

***EDIT ****

GetSummary is in my Business Layer, which is an interface from the datalayer:

public static List<Summary> GetSummary(string query)
        {
            return MessageRepository.GetSummary(query);
        }

this is the interface:

List<Summary> GetSummary(string query);

which is used in the datalayer, taking advantage of dapper:

  public List<Summary> GetSummary(string query)
        {
            using (var block = new TransactionBlock())
            {
                var results =
                    TransactionBlock.Connection.Query<Summary>(query, transaction: TransactionBlock.Transaction)
                        .ToList();
                block.Commit();
                return results;
            }
        }
CSharpNewBee
  • 1,951
  • 6
  • 28
  • 64
  • 1
    Don't create a `StringBuilder` (I assume that's what `sb` is). Instead, create a `PageTotals` object that has the necessary fields, then serialize that. – mason Apr 25 '14 at 13:53
  • 2
    You're serializing a string! You don't serialize strings, you **de** serialize strings to objects. You serialize objects to strings. – Liam Apr 25 '14 at 13:54
  • possible duplicate of [How to write a Json file in C#?](http://stackoverflow.com/questions/16921652/how-to-write-a-json-file-in-c) – Liam Apr 25 '14 at 13:55
  • If your building the JSON by hand, you don't need Json.Net – Liam Apr 25 '14 at 13:57
  • No, you do not need to construct the JSON by hand. That is a **bad** idea. Instead, use a library such as [Json.NET](http://json.codeplex.com/) that can add properties to the JSON on the fly. – mason Apr 25 '14 at 13:58
  • @mason: It's not a bad idea. It's harder but perfectly valid. – Liam Apr 25 '14 at 13:59
  • @Liam I did not say it wouldn't be valid. It said it would be a **bad** idea. The chances of it ending up valid are not good, and it'll be a pain to maintain, and there is NO reason to create JSON by hand when there are perfectly good *free* and *efficient* libraries to create it for you. – mason Apr 25 '14 at 14:03
  • 1
    @CSharpNewBee Regarding your edit, please show the **code** you're using. Also, please clean up your question so that it's not a bunch of hard to follow edits. Make it fully encompass the question. – mason Apr 25 '14 at 14:08
  • Okay, your updated code is working for me. I suspect that what you're copying it from is adding the backslashes, as if you were copying it out of the debugger. Try writing to the console or a log or emailing the results to yourself. – mason Apr 25 '14 at 14:20
  • 1
    Like I said those backslashes are **not in the actual string**. You're just seeing them because that's how the debugger displays strings that contain inner double quotes. Write the string to a log file, or to the [debug console](http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.debug.writeline), or send it to yourself via email. Or test your Web API call. You'll see the backslashes aren't in there. Also, your debugger is showing that you're using the built in `JavaScriptSerializer`. I highly recommend sticking to one serializer (Json.NET being preferred.) – mason Apr 25 '14 at 14:28
  • Provide your `GetSummary()` function. – mason Apr 25 '14 at 16:18
  • Thanks for the down vote peeps :-) – CSharpNewBee Apr 25 '14 at 19:00

3 Answers3

3

You should not create your json by your self. You should let JSON.net do the job for you by passing the object to it.

That takes care of validation and is performant that the StringBuilder you are using.

Create a class with necessary properties and serialize it instead using .JsonConvert(myObj);

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
1

You are creating a JSON and serializing it again as JSON.

That is a bad idea, at least, when you 'just' want JSON.

You have two options:

  1. Keep generating the JSON yourself.
  2. Create an object and serialize it as JSON using JsonConvert.
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    @CSharpNewBee: How do you get that text? Using Visual Studio or an actual file? Visual Studio shows you the escaped string. – Patrick Hofman Apr 25 '14 at 14:16
1

Create a class to store your data, then serialize that.

public class Summary
{
    public int RowNumber {get; set;}
    public int TotalRows {get; set;}
    public int TotalDisplayRows {get; set;}
}

Summary pt=new Summary();
pt.TotalRecords=10;
pt.TotalDisplayRecords=10;
pt.RowNumber=1;
string json=JsonConvert.SerializeObject(pt);
mason
  • 31,774
  • 10
  • 77
  • 121