C#JSON字符串到Web API
目前,在C#中生成JSON字符串并尝试使用WebClient将其发送到web API时,遇到了一个问题 目前我有几种方法,首先是有效的方法。使用邮递员将以下文本数据集发送到API可以正常工作: 邮递员数据集-这适用于邮递员 在C#中,我使用了几种不同的方法来构建此刺,但收效甚微 C#List方法-Newtonsoft.Json.Serialization 首先是构建一个列表,然后使用Newtonsoft.Json来处理它:C#JSON字符串到Web API,c#,json,C#,Json,目前,在C#中生成JSON字符串并尝试使用WebClient将其发送到web API时,遇到了一个问题 目前我有几种方法,首先是有效的方法。使用邮递员将以下文本数据集发送到API可以正常工作: 邮递员数据集-这适用于邮递员 在C#中,我使用了几种不同的方法来构建此刺,但收效甚微 C#List方法-Newtonsoft.Json.Serialization 首先是构建一个列表,然后使用Newtonsoft.Json来处理它: List<Parent> DataList = ne
List<Parent> DataList = new List<Parent>();
List<Child> Form = new List<Child.formvalues>();
var Formvals = new Child.formvalues
{
ActionDescription = Row.ActionDescription,
ActionNotes = Row.ActionNotes,
ActionID = Row.ActionID,
AssetID = Row.AssetID
};
Form.Add(Formvals);
var DataElement = new Parent
{
form_id = AppFormID,
latitude = Lat,
longitude = Long,
status = Row.Status,
form_values = Form
};
DataList.Add(DataElement);
string json = JsonConvert.SerializeObject(DataList.ToArray());
我正在尝试的另一个尝试是以下,我认为这是迄今为止最接近的尝试:
C#-串构造法
这一行的结果是:
{ \"record\":
{
\"form_id\": \"efe4f66f-b57c-4497-a370-25c0f3d8746a\",
\"status\": \"239\",
\"latitude\": -82.638039,
\"longitude\": 27.770787,
\"form_values\":
{
\"833b\": \"99999\",
\"683b\": \"9999999\",
\"fa37\": \"Testing\",
\"b2e3\": \"Testing\"
}
}
}
问题来了
所以问题是,有人能帮我实现我放入POSTMAN的第一个JSON格式吗
更新时间:下午6:40
Web客户端代码c#
AppURLRef在代码中进一步设置,并且在调试器中看起来是正确的。json是测试的结果
var http = new WebClient();
http.Headers.Add(HttpRequestHeader.ContentType, "application/json");
var response = http.UploadString(AppURLRef, "POST", json);
更新结果
我认为这与我的想法非常相似。您可以尝试以下代码来满足要求:
var dd = {
"FirstName": "ABC",
"username": "abc123",
"password": "abc@123",
"Cnumbers": [{
"Home": "0987654321"
}, {
"Company": "7654321"
}]
}
尝试以下方法
public class Record
{
[JsonProperty(PropertyName = "form_id")]
public string FormId { get; set; }
[JsonProperty(PropertyName = "status")]
public string Status { get; set; }
[JsonProperty(PropertyName = "latitude")]
public decimal Latitude { get; set; }
[JsonProperty(PropertyName = "longitude")]
public decimal Longitude { get; set; }
[JsonProperty(PropertyName = "form_values")]
public Dictionary<string, string> FormValues { get; set; }
}
public class RecordContainer
{
[JsonProperty(PropertyName = "record")]
public Record Record { get; set; }
}
我正在使用Newtonsoft Json进行序列化,得到的输出与您所要求的相同。我将尝试使用Newtonsoft.Json进行以下操作
var data = new
{
record = new
{
form_id = "efe4f66f-b57c-4497-a370-25c0f3d8746a",
status = "240",
latitude = -82.638039,
longitude = 27.770787,
form_values = new Dictionary<string, string>();
}
}
data.record.form_values["833b"] = "99999";
data.record.form_values["683b"] = "9999999";
data.record.form_values["fa37"] = "Testing";
data.record.form_values["b2e3"] = "Testing";
尝试此模型后,API仍会抱怨数据不正确-在调试器中停止它会显示与C#-字符串构建方法相同的结果hi@Caz1224,如何使用Web客户端将数据发送到Web API?如果您有代码段,我可以尝试复制该问题。没有问题-我将使用webclient代码段更新主要问题您缺少内容类型标题。请检查Phew中的第二个答案,很高兴它不是那么简单。解决了这个问题,我仍然有我的问题。请按照您的说明使用字符串:-请参阅更新-唯一的问题是833b和683b不能是变量名,因为它们以数字开头。我假设您指的是JsonConvert.SerializeObject(数据);在使用过程中?如果删除这些,我仍然会从服务器上得到反馈,说它不喜欢itI,我必须放弃WebClient(),因为它无法工作。我把RestClient放进去,然后用你的方法生成JSON,它工作得非常好。谢谢你的帮助
"[{\"record\":{\"form_id\":\"efe4f66f-b57c-4497-a370-25c0f3d8746a\",
\"latitude\":-82.638039,
\"longitude\":27.770787,
\"status\":\"240\",
\"form_values\":
[{\"833b\":\"99999\",
\"683b\":\"9999999\",
\"fa37\":\"Testing\",
\"b2e3\":\"Testing\"}]}}]"
var dd = {
"FirstName": "ABC",
"username": "abc123",
"password": "abc@123",
"Cnumbers": [{
"Home": "0987654321"
}, {
"Company": "7654321"
}]
}
public class Record
{
[JsonProperty(PropertyName = "form_id")]
public string FormId { get; set; }
[JsonProperty(PropertyName = "status")]
public string Status { get; set; }
[JsonProperty(PropertyName = "latitude")]
public decimal Latitude { get; set; }
[JsonProperty(PropertyName = "longitude")]
public decimal Longitude { get; set; }
[JsonProperty(PropertyName = "form_values")]
public Dictionary<string, string> FormValues { get; set; }
}
public class RecordContainer
{
[JsonProperty(PropertyName = "record")]
public Record Record { get; set; }
}
var container = new RecordContainer();
container.Record = new Record();
// Code to populate values
JsonConvert.SerializeObject(container);
var data = new
{
record = new
{
form_id = "efe4f66f-b57c-4497-a370-25c0f3d8746a",
status = "240",
latitude = -82.638039,
longitude = 27.770787,
form_values = new Dictionary<string, string>();
}
}
data.record.form_values["833b"] = "99999";
data.record.form_values["683b"] = "9999999";
data.record.form_values["fa37"] = "Testing";
data.record.form_values["b2e3"] = "Testing";
string json = JsonConvert.SerializeObject(data);