Reputation: 19
I tried to figure it out how to produce output of JSON format from API. I have the following class defined :
public class MetaData
{
public string code { get; set; }
public string message { get; set; }
}
public class TSep
{
public string noSep { get; set; }
public string noKartu { get; set; }
public string tglSep { get; set; }
public Peserta peserta { get; set; }
public string tglPulang { get; set; }
public string ppkPelayanan { get; set; }
public string jnsPelayanan { get; set; }
public string klsRawat { get; set; }
public string noMR { get; set; }
public Rujukan rujukan { get; set; }
public string catatan { get; set; }
public string keterangan { get; set; }
public string diagAwal { get; set; }
public string diagnosa { get; set; }
public Poli poli { get; set; }
public Cob cob { get; set; }
public Katarak katarak { get; set; }
public Jaminan jaminan { get; set; }
public Skdp skdp { get; set; }
public string noTelp { get; set; }
public string user { get; set; }
}
public class ResponseSEP
{
public MetaData metadata;
public TSep response;
}
And I also created the controller to produce JSON output as the following :
public ResponseSEP Get(string id)
{
ResponseSEP oSep = new ResponseSEP();
MetaData oMetaData = new MetaData();
oMetaData.code = "200";
oMetaData.message = "sukses";
oSep.metadata = oMetaData;
TSep response = new TSep() ;
response.catatan = "test";
response.diagnosa = "Cholera";
response.jnsPelayanan = "Rawat Inap";
response.noSep = id;
Rujukan rujukan = new Rujukan();
rujukan.noRujukan = "Rujukan" + id;
response.rujukan = rujukan;
oSep.response = response;
return oSep;
}
When I executed the api : http://localhost:31395/api/sep/1234123 , I get the following output :
{
"metadata":{"code":"200","message":"sukses"},
"response":
{
"noSep":"1234123",
"noKartu":null,
"tglSep":null,
"peserta":null,
"tglPulang":null,
"ppkPelayanan":null,
"jnsPelayanan":"Rawat Inap",
"klsRawat":null,
"noMR":null,
"rujukan":
{
"asalRujukan":null,
"tglRujukan":null,
"noRujukan":"Rujukan1234123",
"ppkRujukan":null
},
"catatan":"test",
"keterangan":null,
"diagAwal":null,
"diagnosa":"Cholera",
"poli":null,
"cob":null,
"katarak":null,
"jaminan":null,
"skdp":null,
"noTelp":null,
"user":null
}
}
I would like to produce the JSON output for certain columns only, not all columns in class TSep, like the following.
{
"metadata":{"code":"200","message":"sukses"},
"response":
{
"noSep":"1234123",
"noKartu":null,
"tglSep":null,
"peserta":null,
"tglPulang":null,
"jnsPelayanan":"Rawat Inap",
"klsRawat":null,
"noMR":null,
"rujukan":
{
"asalRujukan":null,
"tglRujukan":null,
"noRujukan":"Rujukan1234123",
"ppkRujukan":null
}
}
}
Any approach to do that ? Appreciate your help.
Upvotes: 2
Views: 1354
Reputation: 18163
You could use JsonIgnore Attribute for ignoring Properties while serializing. For example,
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
[JsonIgnore]
public int Age{get;set;}
}
An alternative Attribute could be ScriptIgnore. Example
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
[ScriptIgnore]
public int Age{get;set;}
}
In either case, the Age Property would be ignored
Update - Based on comment
As per the comment, you only need to ignore if it is null. You could use JsonSerializerSettings.NullValueHandling for the purpose. For example, if student is the object to be serialized,
var json = JsonConvert.SerializeObject(student,
Newtonsoft.Json.Formatting.None,
new JsonSerializerSettings {
NullValueHandling = NullValueHandling.Ignore
});
Where Student is defined as
var student = new Student
{
Id = 1,
Name = "John",
Age = 35
};
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age{get;set;}
}
Upvotes: 1
Reputation: 1362
Generally you have 3 options here. Firstly, you can define your anonymous object on fly and fill it with only fields you want, than serialize this anonymous object.
var requestedDataOnly = new { Metadata = oMetaData, someAnotherField = 10 };
var json = JsonConvert.SerializeObject(requestedDataOnly);
It will produce a JSON with only fields defined inside this object.
Second option is to define an additional DTO class with only fields you want. You can use services like http://json2csharp.com/ to generate DTOs.
public class Metadata
{
public string code { get; set; }
public string message { get; set; }
}
public class Rujukan
{
public object asalRujukan { get; set; }
public object tglRujukan { get; set; }
public string noRujukan { get; set; }
public object ppkRujukan { get; set; }}
public class Response
{
public string noSep { get; set; }
public object noKartu { get; set; }
public object tglSep { get; set; }
public object peserta { get; set; }
public object tglPulang { get; set; }
public string jnsPelayanan { get; set; }
public object klsRawat { get; set; }
public object noMR { get; set; }
public Rujukan rujukan { get; set; }
}
public class RootObject
{
public Metadata metadata { get; set; }
public Response response { get; set; }
}
var rootObject = new RootObject();
var json = JsonConvert.SerializeObject(rootObject);
It allows you to store a DTO object as a separate model and it can be reused in different places inside your codebase.
Third option is to use [JsonIgnore]
property, see another answer to this question.
Upvotes: 1