简体   繁体   English

将数组对象转换为Json作为Json中的一行(数组的序列化)ASP.NET core3.1

[英]convert array object to Json as one line in Json (Serialization of array) ASP.NET core3.1

I'M building Api that need to return json array as response like this :我正在构建需要返回 json 数组作为响应的 Api,如下所示:

"tagList": ["dragons", "training"],

But I got ((each element in separate line )):但我得到了((单独行中的每个元素)):

"tagList": [
        "dragons",
        "training"
    ],

I was using System.Text.Json.Serialization;我正在使用System.Text.Json.Serialization; I tried to change it to Json.net by JsonConvert.SerializeObject(obj) but I got the same result .我试图通过JsonConvert.SerializeObject(obj)将其更改为Json.net ,但我得到了相同的结果。 could anyone help me ,please ?任何人都可以帮助我吗?

since you are using System.Text.Json.Serialization , try this因为您使用的是 System.Text.Json.Serialization ,所以试试这个

 JsonSerializerOptions options = new JsonSerializerOptions();
  options.WriteIndented = false //not using indent, white space, new line, etc.
    
    string jsonString = JsonSerializer.Serialize(obj, options);

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

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