0

We are developing a c# api project. We want to serialize a class with 3 properties but this class has 10 properties inside it. How can i create and convert json this class with 3 properties ? I want to generate a json string with this 3 property.

public class SampleClass
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public string Prop3 { get; set; }
public string Prop4 { get; set; }
public string Prop5 { get; set; }
public string Prop6 { get; set; }
public string Prop7 { get; set; }
public string Prop8 { get; set; }
public string Prop9 { get; set; }
public string Prop10 { get; set; }
}

var myObj = new MyClass
    {
        Prop1 = "Value 1",
        Prop2 = "Value 2",
        Prop3 = "Value 3"
    };
1

1 Answer 1

1

You could use JsonIgnoreAttribute:

public class SampleClass
{
    public string Prop1 { get; set; }

    public string Prop2 { get; set; }

    public string Prop3 { get; set; }

    [JsonIgnore]
    public string Prop4 { get; set; }

    [JsonIgnore]
    public string Prop5 { get; set; }

    [JsonIgnore]
    public string Prop6 { get; set; }

    [JsonIgnore]
    public string Prop7 { get; set; }

    [JsonIgnore]
    public string Prop8 { get; set; }

    [JsonIgnore]
    public string Prop9 { get; set; }

    [JsonIgnore]
    public string Prop10 { get; set; }
}

Reference: How to ignore properties with System.Text.Json

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.