2

Using json.net, I've created a json string which is a serialized version of my data table, and am able to deserialize it back to a data table. Let's say my serialized text looks like this:

[{ username: "alan", firstName: "Alan", lastName: "Johnson", email: "alan@test.com" },
{ username: "allison", firstName: "Allison", lastName: "House", email: "al@test.com" },
{ username: "ryan", firstName: "Ryan", lastName: "Carson", email: "ryan@test.com" } ]

What I'd like to do is add some text to the beginning before the [, or to the end after the ], which would be included in the json text and not hinder the deserialization back to a data table.

I'm using the serialize command, sometimes with a class object as the second (optional) parameter of the serialization command, sometimes not. If I use the second argument, it results in a much more verbose json, which includes table and column definition information.

Either way, I want to possibly add a segment in the json text which will indicate success or failure of the lookup, but will not cause the deserialization to break.

Can anybody suggest a json.net method to do this?

Thanks...

RMittelman
  • 319
  • 3
  • 16
  • What do you mean by "not hinder the deserialization". Should it be *completely ignored*, or should it be possible to be read and parsed? If you want some text that is completely ignored, then Json.NET [supports comments](https://stackoverflow.com/questions/10316997) even though the [JSON standard itself](http://www.json.org/) does not. – dbc Apr 14 '16 at 02:58
  • What I mean is I want a finished string to be something like: { "status" : "success", "data" : [ { all stuff as in record 1 above }, { all stuff as in record 2 above }, { all stuff as in record 3 above } ] } . The information within square brackets is result of serialization. The status is something I add to indicate success, but shouldn't be part of the serialized data. Thanks... – RMittelman Apr 14 '16 at 14:46
  • To further clarify, I want my client to see and react to the "status" value in the response, but only the information in the "data" section should be deserialized. BTW, is a comment (as supported by json.net) included in the response, and can be seen by the receiver? If so, that may work also. Thanks... – RMittelman Apr 14 '16 at 14:55
  • That last comment makes things less clear. I can tell you how to create then deserialize the JSON in your previous comment, but I don't understand *I want my client to see and react to the "status" value in the response, but only the information in the "data" section should be deserialized.* To react to the `"status"` means that its value needs to be deserialized, right? – dbc Apr 14 '16 at 15:06
  • I'm sorry I'm not making this as clear to you as it is to me. Aim is to parse json result for "status" value and if ok, deserialize the "data" section. I can extract the "data" part of the response & use json.net to deserialize that. I just need to figure out how to take the serialized data and surround it with the status info, preserving the "well-formedness" BEFORE returning it back to the client. Thanks...l – RMittelman Apr 14 '16 at 16:16
  • I "think" I've got this working. I retrieve the data, then serialize it using json.net, then before returning it as the response, I add: "{ 'status' : 'success', 'data' : " to the beginning, and " }" to the end. When I receive the response, I extract the status value (in this case "success"). If success is there, I trim all the above text from the response, and deserialize what is left. This seems to work, but I'm sure I'm not doing it the "right" way. Is there a better json.net way to do this? – RMittelman Apr 14 '16 at 16:54

1 Answers1

0
public class Users
{
    public string username { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string email { get; set; }
}

dynamic usercollectionWrapper = new
{           
    userList = new List<Users>
    {    
        new Users()
        {
            username= "alan", firstName= "Alan", lastName= "Johnson", email= "alan@test.com"
        },
        new Users()
        {
            username= "allison", firstName= "Allison", lastName= "House", email= "al@test.com"
        },
        new Users() 
        { 
            username= "ryan", firstName= "Ryan", lastName= "Carson", email= "ryan@test.com" 
        }
    }
};

var output = JsonConvert.SerializeObject(usercollectionWrapper);

Fiddle:https://dotnetfiddle.net/aAZ3Ah

Update:

public class Users
{
    public string username { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string email { get; set; }
}

public class RootUsers
{
    public string status { get; set; }          
    public List<Users> data { get; set; }
}

dynamic usercollectionWrapper = new
{
    add = new RootUsers()
    {
        status = "success",         
        data = new List<Users>
        {    
            new Users()
            {
                username= "alan", firstName= "Alan", lastName= "Johnson", email= "alan@test.com"
            },
            new Users()
            {
                username= "allison", firstName= "Allison", lastName= "House", email= "al@test.com"
            },
            new Users() 
            { 
                username= "ryan", firstName= "Ryan", lastName= "Carson", email= "ryan@test.com" 
            }
        }
    }
};

var output = JsonConvert.SerializeObject(usercollectionWrapper);

Fiddle:https://dotnetfiddle.net/Ic6M1Z

Krunal Mevada
  • 1,637
  • 1
  • 17
  • 28
  • Thanks for the suggestion. So new at json that I'm not sure how to properly phrase the question. I'm serializing a data table, so not sure how building list of class items helps me. Just want an extra status field in the json response, not included in the actual data. Please see my comment above for more details. – RMittelman Apr 14 '16 at 14:52