C# 需要将JSON数据发布到具有丑陋JSON结构的端点
我试图将JSON数据发布到一个端点,该端点要求数据的格式完全符合规范。JSON格式很难看,如果以其他方式发布数据,就会失败 我在一个单独的IEnumerableObject中拥有所有必需的数据,我需要以下面描述的JSON格式准备这些数据 我尝试了各种方法来格式化数据,以符合此列表,但我认为必须有一种更简单的方法 jsonConverter to c#类工具为我提供了以下嵌套类结构C# 需要将JSON数据发布到具有丑陋JSON结构的端点,c#,.net,linq,C#,.net,Linq,我试图将JSON数据发布到一个端点,该端点要求数据的格式完全符合规范。JSON格式很难看,如果以其他方式发布数据,就会失败 我在一个单独的IEnumerableObject中拥有所有必需的数据,我需要以下面描述的JSON格式准备这些数据 我尝试了各种方法来格式化数据,以符合此列表,但我认为必须有一种更简单的方法 jsonConverter to c#类工具为我提供了以下嵌套类结构 internal class ActivityModel { [JsonProperty("$type")]
internal class ActivityModel
{
[JsonProperty("$type")]
public string Type { get; set; }
public Properties Properties { get; set; }
}
internal class Properties
{
[JsonProperty("$type")]
public string Type { get; set; }
[JsonProperty("$values")]
public List<Value> Values { get; set; }
}
internal class Value
{
[JsonProperty("$type")]
public string Type { get; set; }
public JObject Name { get; set; }
public JObject ValueValue { get; set; }
}
到目前为止,我一直在努力:
var partyID = builtResponse.First().PartyId;
string muf_1 = builtResponse.First().MUF_1;
string description = builtResponse.First().Description;
string courseCode = builtResponse.First().Product_Code;
DateTime dateTime = DateTime.Now;
ActivityModel activityModel = new ActivityModel();
activityModel = new ActivityModel()
{
Type = "Asi.Soa.Core.DataContracts.GenericEntityData, Asi.Contracts",
Properties =
{
Type = "Asi.Soa.Core.DataContracts.GenericPropertyDataCollection, Asi.Contracts",
Values =
{
new Value()
{
Type = "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
Name = "Activity_Type", ValueValue = "Education"
},
new Value()
{
Type = "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
Name = "PartyId", ValueValue = partyID
},
new Value()
{
Type = "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
Name = "Description", ValueValue = description
}
}
}
};
代码构建良好,没有构建时错误,但现在我得到一个未设置为引用错误的对象。不是很有帮助 试试这个
public class Value
{
[JsonProperty("$type")]
public string Type { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
public class Properties
{
[JsonProperty("$type")]
public string Type { get; set; }
[JsonProperty("$values")]
public List<Value> Values { get; set; }
}
public class ActivityModel
{
[JsonProperty("$type")]
public string Type { get; set; }
public Properties Properties { get; set; }
}
公共类值
{
[JsonProperty(“$type”)]
公共字符串类型{get;set;}
公共字符串名称{get;set;}
公共字符串值{get;set;}
}
公共类属性
{
[JsonProperty(“$type”)]
公共字符串类型{get;set;}
[JsonProperty(“$values”)]
公共列表值{get;set;}
}
公共类活动模型
{
[JsonProperty(“$type”)]
公共字符串类型{get;set;}
公共属性属性{get;set;}
}
您尝试过的代码及其有问题的输出在哪里?如果您有Asi.Contracts程序集,它将与NewtonSoft JSON一起开箱即用(无需定义类型属性),因为$type字段是NewtonSoft JSON的一个特殊标记,以了解目标类型的原因。我想在服务器外部调用它。结束REST@glenebob,我已经添加了一段代码,但是这个错误对我来说没有意义。任何关于我所做的错误的见解?请添加您得到的异常的全文。我不相信抛出异常的代码存在;请加上。请添加实际尝试生成JSON的代码。我们需要。我试过了,但仍然得到一个未设置为引用错误的对象
public class Value
{
[JsonProperty("$type")]
public string Type { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
public class Properties
{
[JsonProperty("$type")]
public string Type { get; set; }
[JsonProperty("$values")]
public List<Value> Values { get; set; }
}
public class ActivityModel
{
[JsonProperty("$type")]
public string Type { get; set; }
public Properties Properties { get; set; }
}