2

下面是我从端点收到的一段 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。

谢谢,瑞安

4

4 回答 4

2

addresses可以反序列化为Dictionary<string,List<YourClass>>where YourClassholdversionaddresss.

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;
}
于 2013-01-16T23:00:10.097 回答
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);
}
于 2013-01-16T22:25:02.847 回答
0

为什么不将网络名称制作成字典,并带有网络名称的键?然后你可以迭代它。

于 2013-01-16T22:28:49.137 回答
-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);
于 2013-01-16T22:39:22.773 回答