下面是我从端点收到的json部分。
如果你看下面的Json,'User-Defined-Network-Name'是一个自定义节点,名称每次都会改变。
如何为此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。
谢谢, 莱恩
答案 0 :(得分:2)
addresses
可以反序列化为类似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;
}
答案 1 :(得分:0)
您可以利用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);
}
答案 2 :(得分:0)
为什么不使用网络名称密钥将网络名称设为字典? 然后你可以迭代它。
答案 3 :(得分:-1)
我不建议使用JavaScriptSerializer
,因为它已被弃用。如果您需要第三方解决方案,JSON.Net
与我听到的相当不错。
但是,我对依赖关系很奇怪,所以如果它不存在,我通常会自己滚动。幸运的是,由于来自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);