0

The following class is just an entity for registration.

>   class registration
>     {
>     public string email
>             {
>                 get;
>                 set;
>             }
>             public string birthday
>             {
>                 get;
>                 set;
>             }
>             public string first_name
>             {
>                 get;
>                 set;
>             }
>     }

There is a signup method where i am processing the values which i got from the user.I have not included full code here.just give a brief information.I will process the fields and convert one json format.I will send the json input to webapi service for further process.

class RegisterProcess { public void SignUp(registration regObj) {

        JProperty UserProfile = new JProperty("RegisterProfile",
                               new JObject(
                               new JProperty("email", regObj.email),
                               new JProperty("birthday", regObj.birthday),
                               new JProperty("first_name", regObj.first_name))); } }

Some users might enter values for all fields & some of them will enter only for few fields.I want to insert only field which has a value.I want to dynamically generate JProperty UserProfile stuff.(for example:If an user enter only email&first_name,I want to create json only for those field.Null value should not be included.How to dynamically generate Json format.any ideas?

Scenario :: Registration entity - I will use for registration process as well as updateprofile process.During registration user might enter email,first_name,birthday.later he want to update only email field.He will enter data for that field alone.At that time i want to generate json rpoperty only for email field.it should not disturb[first_name and birthday] field.if i send [registration MyReg = new registration { email = "[email protected]"};],It will set [first_name and birthday field] as null.It shouldn't set null for those fields.it should retain the value which i entered while registration.i hope you got it

1 Answer 1

1

Assuming you have the Newtonsoft JSON library installed:

registration MyReg = new registration { email = "[email protected]", birthday="today", first_name="bob" };

String JsonReg = Newtonsoft.Json.JsonConvert.SerializeObject(MyReg);

Isn't this all you need? On the receiving end, missing (null) fields will just be skipped.

Sign up to request clarification or add additional context in comments.

3 Comments

I will use the same entity while updating the user profile.so i don't enter the value in the email field while updating the profile,It should take the earlier one.I mean the data which i entered while registration.
I don't understand what you mean.
Registration entity - I will use for registration process as well as updateprofile process.During registration user might enter email,first_name,birthday.later he want to update only email field.He will enter data for that field alone.At that time i want to generate json rpoperty only for email field.it should not disturb[first_name and birthday] field.if i send [registration MyReg = new registration { email = "[email protected]"};]It will set [first_name and birthday field] as null.It shouldn't set null for those fields.it should retain the value which i entered while registration.i hope you got it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.