如何向JSON请求体中的键传递空值

huangapple go评论71阅读模式
英文:

How to pass a null value to a key in a JSON body request

问题

要在API的POST请求中传递一个空值给一个键,你可以像下面这样做。

例如,我想传递以下JSON数据,即ExpTeamID为null。

{
   "ID": 162617,
   "TextKey": "107737",
   "Exp": null,
   "TeamID": null
}

这个结果在Postman中是被接受的,但是当我尝试在C#代码中使用相同的方式传递时,我的JSON内容变得无效。

long idvalue = 162617;
string textkeyvalue = "107737";
string expvalue = null;
long? teamIDvalue = null;

string postData = "{\"ID\":" + idvalue + ",\"TextKey\":\"" + textkeyvalue + "\",\"Exp\":" + expvalue + ",\"TeamID\":" + teamIDvalue + "}";

这给我以下输出。

{
   "ID": 162617,
   "TextKey": "107737",
   "Exp": "",
   "TeamID":
}

我的请求由于无效的JSON主体而失败。那么,如何传递这种类型的null数据或null关键字呢?

注意:在我的API中,所有键值对都是强制的,所以如果它们为null,我不能省略它们。

我只想以以下格式传递数据。

{
   "ID": 162617,
   "TextKey": "107737",
   "Exp": null,
   "TeamID": null
}
英文:

I want to pass a null value to a key using a POST request in an API.

For example, I want to pass the below JSON data. That is, Exp and TeamID is null.

<!-- language: json -->
{
"ID":162617,
"TextKey":"107737",
"Exp":null,
"TeamID":null
}

The result is accepted in Postman, but when I tried passing the same using C# code below, my JSON content becomes invalid.

long idvalue = 162617;
string textkeyvalue = &quot;107737&quot;;
string expvalue = null;
long? teamIDvalue = null;

string postData = &quot;{\&quot;ID\&quot;:&quot; + idvalue + &quot;,\&quot;TextKey\&quot;:\&quot;&quot; + textkeyvalue + &quot;\&quot;,\&quot;Exp\&quot;:\&quot;&quot; + expvalue + &quot;\&quot;,\&quot;TeamID\&quot;:\&quot;&quot; + teamIDvalue + &quot;\&quot;}&quot;;

Which gives me the following output.

<!-- language: json -->
{
"ID":162617,
"TextKey":"107737",
"Exp":"",
"TeamID":
}

And my request fails due to the invalid JSON body. So how do i pass this sort of null data or null keyword?

Note : All the Key value pairs are mandatory in my API, so I cannot omit them if they are null.

I just want to pass the data in the below format.

<!-- language: json -->
{
"ID":162617,
"TextKey":"107737",
"Exp":null,
"TeamID":null
}

答案1

得分: 3

以下是翻译好的代码部分:

要回答你的问题,以便为 null 值编写文本,你可以这样做:

var result = "my value is " + (strValue ?? "null");
或者要添加引号:

var result = "my value is " + (strValue == null ? "null" : $"\"{strValue}\"");
你还可以创建静态辅助方法以使其更容易:

static string write(string str) => str == null ? "null" : $"\"{str}\"";
static string write(long? value) => value == null ? "null" : value.ToString();
因此,在你的示例中,它变成了:

string postData = "{"ID":" + idvalue + ","TextKey":" + write(textkeyvalue) +
                  ","Exp":" + write(expvalue) + ","TeamID":" +
                  write(teamIDvalue) + "}";

更好的解决方案!

创建一个用于数据模型的类,并使用库(例如 System.Text.Json)来对其进行序列化,如下所示:

public class MyData
{
   public long ID { get; set; }
   public string TextKey { get; set; }
   public string Exp { get; set; }
   public long? TeamID { get; set; }
}

// 构造模型
var data = new MyData()
{
   ID = 162617,
   TextKey = "107737",
   Exp = null,
   TeamID = null,
}

// 序列化为 JSON
var result = System.Text.Json.JsonSerializer.Serialize(data);

<details>
<summary>英文:</summary>

To answer your question, to write text for a null value, you can do this:

```cs
var result = &quot;my value is &quot; + (strValue ?? &quot;null&quot;);

Or to add quotes:

var result = &quot;my value is &quot; + (strValue == null ? &quot;null&quot; : $&quot;\&quot;{strValue}\&quot;&quot;);

You can also create static helper methods to make it easier:

static string write(string str) =&gt; str == null ? &quot;null&quot; : $&quot;\&quot;{str}\&quot;&quot;;
static string write(long? value) =&gt; value == null ? &quot;null&quot; : value.ToString();

So in your example, it becomes:

string postData = &quot;{\&quot;ID\&quot;:&quot; + idvalue + &quot;,\&quot;TextKey\&quot;:&quot; + write(textkeyvalue) +
                  &quot;,\&quot;Exp\&quot;:&quot; + write(expvalue) + &quot;,\&quot;TeamID\&quot;:&quot; +
                  write(teamIDvalue) + &quot;}&quot;;

<br/>

Better solution!

Create a class for the data model, and use a library (for example, System.Text.Json) to serialize it, like so:

public class MyData
{
   public long ID { get; set; }
   public string TextKey { get; set; }
   public string Exp { get; set; }
   public long? TeamID { get; set; }
}

// Construct model
var data = new MyData()
{
   ID = 162617,
   TextKey = &quot;107737&quot;,
   Exp = null,
   TeamID = null,
}

// Serialize to JSON
var result = System.Text.Json.JsonSerializer.Serialize(data);

答案2

得分: 3

gepa's answer 答案非常准确,但你也可以序列化一个匿名对象:

long idvalue = 162617;
string textkeyvalue = "107737";
string? expvalue = null;
long? teamIDvalue = null;

string postData = System.Text.Json.JsonSerializer.Serialize(new
{
    ID = idvalue,
    TextKey = textkeyvalue,
    Exp = expvalue,
    TeamID = teamIDvalue,
});
英文:

gepa’s answer is right on the money, but you can serialize an anonymous object, too:

long idvalue = 162617;
string textkeyvalue = &quot;107737&quot;;
string? expvalue = null;
long? teamIDvalue = null;

string postData = System.Text.Json.JsonSerializer.Serialize(new
{
    ID = idvalue,
    TextKey = textkeyvalue,
    Exp = expvalue,
    TeamID = teamIDvalue,
});

huangapple
  • 本文由 发表于 2023年3月31日 19:50:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定