![](/img/trans.png)
[英]How do I deserialize json to c# model if the json object has a name starting with an int
[英]How do I deserialize a custom node in Json to c#, whose name keeps changing?
以下是我從端點收到的json部分。
如果您查看下面的Json,“用戶定義的網絡名稱”是一個自定義節點,並且名稱每次都會更改。
如何為此Json定義C#對象?
"addresses": {
"public": [{
"version": 6,
"address": "2005:4600:788e:0910:1a72:81c0:ff03:c7y6"
},
{
"version": 4,
"address": "197.68.xx.xxx"
}],
"private": [{
"version": 4,
"address": "10.xx.xx.xxx"
}],
"User-Defined-Network-Name": [{
"version": 4,
"address": "192.xxx.x.xxx"
}]
}
這就是我走了多遠-
[Serializable]
public class Addresses
{
public List<Public> @public { get; set; }
public List<Private> @private { get; set; }
}
我正在使用'JavascriptSerializer'類反序列化json。
謝謝,瑞安
可以將addresses
反序列化為Dictionary<string,List<YourClass>>
類的類型Dictionary<string,List<YourClass>>
其中YourClass
保存version
和addresss
。
var obj = new JavaScriptSerializer().Deserialize<Root>(jsonstring);
-
public class Root
{
public Dictionary<string,List<VersionAddress>> addresses;
//Your other fields/properties
}
public class VersionAddress
{
public string version;
public string address;
}
您可以利用C#的動態特性:
// this could come from user input:
string userDefinedName = "User-Defined-Network-Name";
string json =“您的JSON即將出現”;
var serializer = new JavaScriptSerializer();
dynamic result = serializer.DeserializeObject(json);
int version = result["addresses"][userDefinedName][0]["version"];
string address = result["addresses"][userDefinedName][0]["address"];
Console.WriteLine(version);
Console.WriteLine(address);
如果您想遍歷結果:
foreach (dynamic item in result["addresses"][userDefinedName])
{
int version = item["version"];
string address = item["address"];
Console.WriteLine(version);
Console.WriteLine(address);
}
為什么不使用網絡名稱關鍵字將網絡名稱設為字典? 然后,您可以對其進行迭代。
我不建議使用JavaScriptSerializer
,因為它已被棄用。 如果您需要第三方解決方案,那么據我JSON.Net
, JSON.Net
相當不錯。
但是,我對依賴項很奇怪,因此如果不存在依賴項,我通常會自己滾動。 幸運的是,由於System.Runtime.Serialization
命名空間中的DataContractJsonSerializer
,這System.Runtime.Serialization
不太難。
您需要做的就是首先以嵌套方式定義所有對象:
using System.Reflection;
using System.Runtime.Serialization; // You will have to add a reference
using System.Runtime.Serialization.Json; // to System.Runtime.Serialization.dll
[DataContract]
public class AddressInfo
{
[DataMember(Name = "address")]
public string Address { get; set; }
[DataMember(Name = "version")]
public int Version { get; set; }
}
[DataContract]
public class AddressList
{
[DataMember(Name = "public")]
public IEnumerable<AddressInfo> Public { get; set; }
[DataMember(Name = "private")]
public IEnumerable<AddressInfo> Private { get; set; }
[DataMember(Name = "User-Defined-Network-Name")]
public IEnumerable<AddressInfo> UserDefined { get; set; }
}
然后使用幾種輔助方法進行反序列化:
// This will change the DataMember.Name at runtime!
// This will only work if you know the node name in advance.
static void SetUserDefinedNodeName(string userDefinedNodeName)
{
var type = typeof(AddressList);
var property = type.GetProperty("UserDefined", BindingFlags.Default);
var attribute = property.GetCustomAttribute<DataMemberAttribute>();
if (attribute != null)
attribute.Name = userDefinedNodeName;
}
static T Deserialize<T>(string jsonText, string userDefinedNodeName)
{
SetUserDefinedNodeName(userDefinedName);
var jsonBytes = Encoding.UTF8.GetBytes(jsonText);
using (var stream = new MemoryStream(jsonBytes))
{
var serializer = new DataContractJsonSerializer(typeof(T));
var obj = serializer.ReadObject(stream) as T;
return obj;
}
}
然后像這樣使用它:
var jsonText = // get your json text somehow
var addressList = Deserialize<AddressList>(jsonText);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.