英文:
How to pass a null value to a key in a JSON body request
问题
要在API的POST请求中传递一个空值给一个键,你可以像下面这样做。
例如,我想传递以下JSON数据,即Exp和TeamID为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 = "107737";
string expvalue = null;
long? teamIDvalue = null;
string postData = "{\"ID\":" + idvalue + ",\"TextKey\":\"" + textkeyvalue + "\",\"Exp\":\"" + expvalue + "\",\"TeamID\":\"" + teamIDvalue + "\"}";
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 = "my value is " + (strValue ?? "null");
Or to add quotes:
var result = "my value is " + (strValue == null ? "null" : $"\"{strValue}\"");
You can also create static helper methods to make it easier:
static string write(string str) => str == null ? "null" : $"\"{str}\"";
static string write(long? value) => value == null ? "null" : value.ToString();
So in your example, it becomes:
string postData = "{\"ID\":" + idvalue + ",\"TextKey\":" + write(textkeyvalue) +
",\"Exp\":" + write(expvalue) + ",\"TeamID\":" +
write(teamIDvalue) + "}";
<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 = "107737",
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 = "107737";
string? expvalue = null;
long? teamIDvalue = null;
string postData = System.Text.Json.JsonSerializer.Serialize(new
{
ID = idvalue,
TextKey = textkeyvalue,
Exp = expvalue,
TeamID = teamIDvalue,
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论