简体   繁体   English

通过仅显示特定字段来格式化JSON输出

[英]Formatting JSON output by only show specific field

I tried to figure it out how to produce output of JSON format from API. 我试图弄清楚如何从API产生JSON格式的输出。 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 : 我还创建了控制器以产生JSON输出,如下所示:

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 : 当我执行api时: http:// localhost:31395 / api / sep / 1234123 ,我得到以下输出:

{

    "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. 我只想为某些列而不是类TSep中的所有列生成JSON输出,如下所示。

{
"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. 感谢您的帮助。

You could use JsonIgnore Attribute for ignoring Properties while serializing. 您可以使用JsonIgnore Attribute在序列化时忽略Properties。 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 . 替代属性可以是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. 根据注释,如果它为null,则只需忽略。 You could use JsonSerializerSettings.NullValueHandling for the purpose. 您可以为此目的使用JsonSerializerSettings.NullValueHandling 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;}
} 

Generally you have 3 options here. 通常,您在这里有3个选项。 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. 它将生成仅在此对象内定义的字段的JSON。

Second option is to define an additional DTO class with only fields you want. 第二种选择是定义一个仅包含所需字段的附加DTO类。 You can use services like http://json2csharp.com/ to generate DTOs. 您可以使用http://json2csharp.com/之类的服务来生成DTO。

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. 它允许您将DTO对象存储为单独的模型,并且可以在代码库内的不同位置重复使用。

Third option is to use [JsonIgnore] property, see another answer to this question. 第三种选择是使用[JsonIgnore]属性,请参见此问题的另一个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM