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

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

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

问题

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

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

  1. {
  2. "ID": 162617,
  3. "TextKey": "107737",
  4. "Exp": null,
  5. "TeamID": null
  6. }

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

  1. long idvalue = 162617;
  2. string textkeyvalue = "107737";
  3. string expvalue = null;
  4. long? teamIDvalue = null;
  5. string postData = "{\"ID\":" + idvalue + ",\"TextKey\":\"" + textkeyvalue + "\",\"Exp\":" + expvalue + ",\"TeamID\":" + teamIDvalue + "}";

这给我以下输出。

  1. {
  2. "ID": 162617,
  3. "TextKey": "107737",
  4. "Exp": "",
  5. "TeamID":
  6. }

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

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

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

  1. {
  2. "ID": 162617,
  3. "TextKey": "107737",
  4. "Exp": null,
  5. "TeamID": null
  6. }
英文:

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.

  1. long idvalue = 162617;
  2. string textkeyvalue = &quot;107737&quot;;
  3. string expvalue = null;
  4. long? teamIDvalue = null;
  5. 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

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

  1. 要回答你的问题,以便为 null 值编写文本,你可以这样做:
  2. var result = "my value is " + (strValue ?? "null");
  1. 或者要添加引号:
  2. var result = "my value is " + (strValue == null ? "null" : $"\"{strValue}\"");
  1. 你还可以创建静态辅助方法以使其更容易:
  2. static string write(string str) => str == null ? "null" : $"\"{str}\"";
  3. static string write(long? value) => value == null ? "null" : value.ToString();
  1. 因此,在你的示例中,它变成了:
  2. string postData = "{"ID":" + idvalue + ","TextKey":" + write(textkeyvalue) +
  3. ","Exp":" + write(expvalue) + ","TeamID":" +
  4. write(teamIDvalue) + "}";

更好的解决方案!

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

  1. public class MyData
  2. {
  3. public long ID { get; set; }
  4. public string TextKey { get; set; }
  5. public string Exp { get; set; }
  6. public long? TeamID { get; set; }
  7. }
  8. // 构造模型
  9. var data = new MyData()
  10. {
  11. ID = 162617,
  12. TextKey = "107737",
  13. Exp = null,
  14. TeamID = null,
  15. }
  16. // 序列化为 JSON
  17. var result = System.Text.Json.JsonSerializer.Serialize(data);
  1. <details>
  2. <summary>英文:</summary>
  3. To answer your question, to write text for a null value, you can do this:
  4. ```cs
  5. var result = &quot;my value is &quot; + (strValue ?? &quot;null&quot;);

Or to add quotes:

  1. 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:

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

So in your example, it becomes:

  1. string postData = &quot;{\&quot;ID\&quot;:&quot; + idvalue + &quot;,\&quot;TextKey\&quot;:&quot; + write(textkeyvalue) +
  2. &quot;,\&quot;Exp\&quot;:&quot; + write(expvalue) + &quot;,\&quot;TeamID\&quot;:&quot; +
  3. 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:

  1. public class MyData
  2. {
  3. public long ID { get; set; }
  4. public string TextKey { get; set; }
  5. public string Exp { get; set; }
  6. public long? TeamID { get; set; }
  7. }
  8. // Construct model
  9. var data = new MyData()
  10. {
  11. ID = 162617,
  12. TextKey = &quot;107737&quot;,
  13. Exp = null,
  14. TeamID = null,
  15. }
  16. // Serialize to JSON
  17. var result = System.Text.Json.JsonSerializer.Serialize(data);

答案2

得分: 3

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

  1. long idvalue = 162617;
  2. string textkeyvalue = "107737";
  3. string? expvalue = null;
  4. long? teamIDvalue = null;
  5. string postData = System.Text.Json.JsonSerializer.Serialize(new
  6. {
  7. ID = idvalue,
  8. TextKey = textkeyvalue,
  9. Exp = expvalue,
  10. TeamID = teamIDvalue,
  11. });
英文:

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

  1. long idvalue = 162617;
  2. string textkeyvalue = &quot;107737&quot;;
  3. string? expvalue = null;
  4. long? teamIDvalue = null;
  5. string postData = System.Text.Json.JsonSerializer.Serialize(new
  6. {
  7. ID = idvalue,
  8. TextKey = textkeyvalue,
  9. Exp = expvalue,
  10. TeamID = teamIDvalue,
  11. });

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:

确定