我有一個需要發布到網關的 json 物件。它看起來如下。
{
"client_id": "test",
"non_personalized_ads": false,
"events": [
{
"name": "test",
"params": {
"items": [
{
"itemitem1": "itemitem1value",
"itemitem1number": 1
}
],
"stringparm1": "stringValue1",
"intparm1": 1
}
}
]
}
我正在嘗試在我的 C# 專案中將其創建為物件類。問題是這些引數的名稱都是可配置的。所以這些都不是靜態屬性名稱。
- "itemitem1": "itemitem1 值",
- “專案 1 號”:1
- "stringparm1": "stringValue1",
- “intparm1”:1
因此,為了實作這一目標,我使用了 Dictionary<string, object>,因為值可以是數字或字串,我使用了 Object。
public class EventParameter
{
public Dictionary<string, object> Parameters { get; set; }
public Dictionary<string, object> Items { get; set; }
public EventParameter()
{
Parameters = new Dictionary<string, object>();
Items = new Dictionary<string, object>();
}
public void AddEventParameter(string name, object value)
{
Parameters.Add(name, value);
}
}
不幸的是,當我對此進行 json 決議時,我得到了。這是不正確的,因為我的引數應該在 parms 下而不是在他們自己的物件下。
{
"client_id": "test",
"non_personalized_ads": false,
"events": [
{
"name": "hit_event",
"params": {
"Parameters": {
"TestString": "test",
"testInt": 1
},
"Items": {
"test": 1,
"test2": 1,
"test3": "test"
}
}
}
]
}
那么如何讓引數上升到引數?感覺 parms 需要是一個 Dictionary<string, object> 但是它會丟失它的專案串列
"Parameters": {
"TestString": "test",
"testInt": 1
},
Im assuming there's something missing in my model design. I have been looking at this too long i cant come up with the solution. My other idea was could this be done by adding a custom json sterilizer?
Let me know if you want my full model design or the unit test I am using to test with.
unit test
[Fact]
public void Test1()
{
var postData = new Event() { Name = "EventName" };
postData.EventParameters.AddEventParameter("TestString", "test");
postData.EventParameters.AddEventParameter("testInt", 1);
postData.EventParameters.Items = new Dictionary<string, object>()
{
{ "test", 1 },
{ "test2", 1 },
{ "test3", "test" },
};
var data = new EventData()
{
ClientId = "test",
Events = new []
{
postData
}
};
var options = new JsonSerializerOptions { IgnoreNullValues = true };
var hold =JsonSerializer.Serialize(data);
// add breakpoint here
int i = 1;
}
uj5u.com熱心網友回復:
這是一個序列化為您顯示的第一個 json 的類:
class X
{
public string client_id {get; set;}
public bool non_personalized_ads {get; set;}
public List<EventParameter> events { get;set;}
}
class EventParameter {
public string name {get; set;}
public Dictionary<string, object> @params {get; set;}
}
測驗代碼:
var x = new X
{
client_id = "test",
non_personalized_ads = false,
events = new List<EventParameter>{
new EventParameter
{
name = "test1",
@params = new Dictionary<string, object>{
{ "items", new Dictionary<string, object> {
{"itemitem1", "itemitem1value"},
{"itemitem1number", 1}
}},
{ "stringparm1", "stringValue1"},
{"intparm1", 1}
}
}
}
};
var json = System.Text.Json.JsonSerializer.Serialize(x, options: new System.Text.Json.JsonSerializerOptions{WriteIndented = true});
Console.WriteLine(json);
uj5u.com熱心網友回復:
嘗試這個
var events = JsonConvert.DeserializeObject<Root>(json);
測驗
json=JsonConvert.SerializeObject(events, Newtonsoft.Json.Formatting.Indented);
{
"client_id": "test",
"non_personalized_ads": false,
"events": [
{
"name": "test",
"params": {
"items": [
{
"itemitem1": "itemitem1value",
"itemitem1number": "1"
}
],
"stringparm1": "stringValue1",
"intparm1": 1
}
}
]
}
班級
public class Root
{
public string client_id { get; set; }
public bool non_personalized_ads { get; set; }
public List<Event> events { get; set; }
}
public class Event
{
public string name { get; set; }
public Params @params { get; set; }
}
public class Params
{
public List<Dictionary<string, string>> items { get; set; }
public string stringparm1 { get; set; }
public int intparm1 { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/447769.html
