我想將以下內容轉換為C#課程,有人可以幫忙嗎?我嘗試了許多不同的突變,但沒有一個可以真正映射Params到類。
{
"method": "update",
"params": [
true,
{
"y": [
[
"3",
"5"
],
[
"1",
"2"
]
],
"x": [
[
"1",
"2"
],
[
"2",
"1"
]
],
"id": 1111,
"update": 164227,
"current": 164227
},
"TESTING"
],
"id": null
}
以下是到目前為止我可以生成的內容,但不幸的是,它無法映射Params到類,我想在串列中獲取 X 和 Y 的值:
public class Data
{
[JsonProperty("method")]
public string Method { get; set; }
[JsonProperty("params")]
public List<ParamTest> Params { get; set; }
[JsonProperty("id")]
public object Id { get; set; }
}
public class ParamClass
{
[JsonProperty("x")]
public string[][] X { get; set; }
[JsonProperty("x")]
public string[][] Y { get; set; }
}
public struct ParamTest
{
public bool Bool;
public List<ParamClass> ParamClass;
public string String;
public static implicit operator ParamTest(bool Bool) => new ParamTest { Bool = Bool };
public static implicit operator ParamTest(List<ParamClass> ParamClass) => new ParamTest { ParamClass = ParamClass };
public static implicit operator ParamTest(string String) => new ParamTest { String = String };
}
uj5u.com熱心網友回復:
由于非結構化資料結構,必須Object用于反序列化。
使用以下類
public class JsonData
{
public string Method { get; set; }
public List<Object> Params { get; set; }
public int? ID { get; set; }
}
public class Params
{
public string[][] X { get; set; }
public string[][] Y { get; set; }
}
然后現在使用
var data = JsonConvert.DeserializeObject<JsonData>(json);
var paramsData = JsonConvert.DeserializeObject<Params>(data.Params[1].ToString());
uj5u.com熱心網友回復:
試試這個。它在 Visual Studio 中測驗并正常作業
var po = JObject.Parse(json);
Data data = new Data { Id = (string)po["id"], Method = (string)po["method"]
var pars = new ParamObject { };
foreach (var element in (JArray)po["params"])
{
var type = element.GetType().Name;
if (element.GetType().Name == "JValue")
{
if ((string)element == "TESTING")
pars.Testing = (string)element;
else pars.IsTrue = (bool)element;
}
else if (element.GetType().Name == "JObject")
{
pars.XYElement = element.ToObject<XYElement>();
}
}
data.Pars = pars;
班級
public partial class Data
{
[JsonProperty("method")]
public string Method { get; set; }
[JsonProperty("params")]
public ParamObject Pars { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
}
public partial class ParamObject
{
[JsonProperty("XYElement")]
public XYElement XYElement { get; set; }
[JsonProperty("IsTrue")]
public bool IsTrue { get; set; }
[JsonProperty("TESTING")]
public string Testing { get; set; }
}
public partial class XYElement
{
[JsonProperty("y")]
public List<List<long>> Y { get; set; }
[JsonProperty("x")]
public List<List<long>> X { get; set; }
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("update")]
public long Update { get; set; }
[JsonProperty("current")]
public long Current { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/412324.html
標籤:
