如何解析Json.NET多态对象?
我编写了一个 Web 服务,用于发送和返回使用 Json.NET 创建的 json。我已经包含了类型名称,它允许多态性。通过一点黑客攻击,我已经在 silverlight 客户端上使用了它,但我不知道如何让它在 javascript 客户端上工作。
如何使用 javascript 解析它?
{
"$type": "MyAssembly.Zoo, MyAssembly",
"ID": 1,
"Animals": [
{
"$type": "MyAssembly.Dog, MyAssembly",
"LikesBones": true,
"Name": "Fido"
},
{
"$type": "MyAssembly.Cat, MyAssembly",
"LikesMice": false,
"Name": "Felix"
}
]
}
以下是 C# 类:
public class Animal
{
public string Name { get; set; }
}
public class Dog : Animal
{
public bool LikesBones { get; set; }
}
public class Cat : Animal
{
public bool LikesMice { get; set; }
}
public class Zoo
{
public int ID { get; set; }
private List<Animal> m_Animals = new List<Animal>();
public List<Animal> Animals { get { return m_Animals; } set { m_Animals = value; } }
public static void Test1()
{
Zoo z1 = new Zoo() { ID = 1 };
z1.Animals.Add(new Dog() { Name = "Fido", LikesBones = true });
z1.Animals.Add(new Cat() { Name = "Felix", LikesMice = false });
var settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.Objects;
string s1 = JsonConvert.SerializeObject(z1, Formatting.Indented, settings);
Debug.WriteLine(s1);
var z2 = JsonConvert.DeserializeObject<Zoo>(s1, settings);
foreach (Animal a in z2.Animals)
{
if (a is Dog)
Debug.WriteLine(((Dog)a).LikesBones);
else if (a is Cat)
Debug.WriteLine (((Cat)a).LikesMice);
else
Debug.WriteLine("error");
}
}
}
I've written a web service that sends and returns json created with Json.NET. I've included typenames, which allows polymorphism. With a bit of hacking, I've got this working with a silverlight client, but I don't know how to make it work for javascript clients.
How can I parse this using javascript?
{
"$type": "MyAssembly.Zoo, MyAssembly",
"ID": 1,
"Animals": [
{
"$type": "MyAssembly.Dog, MyAssembly",
"LikesBones": true,
"Name": "Fido"
},
{
"$type": "MyAssembly.Cat, MyAssembly",
"LikesMice": false,
"Name": "Felix"
}
]
}
Here are the c# classes:
public class Animal
{
public string Name { get; set; }
}
public class Dog : Animal
{
public bool LikesBones { get; set; }
}
public class Cat : Animal
{
public bool LikesMice { get; set; }
}
public class Zoo
{
public int ID { get; set; }
private List<Animal> m_Animals = new List<Animal>();
public List<Animal> Animals { get { return m_Animals; } set { m_Animals = value; } }
public static void Test1()
{
Zoo z1 = new Zoo() { ID = 1 };
z1.Animals.Add(new Dog() { Name = "Fido", LikesBones = true });
z1.Animals.Add(new Cat() { Name = "Felix", LikesMice = false });
var settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.Objects;
string s1 = JsonConvert.SerializeObject(z1, Formatting.Indented, settings);
Debug.WriteLine(s1);
var z2 = JsonConvert.DeserializeObject<Zoo>(s1, settings);
foreach (Animal a in z2.Animals)
{
if (a is Dog)
Debug.WriteLine(((Dog)a).LikesBones);
else if (a is Cat)
Debug.WriteLine (((Cat)a).LikesMice);
else
Debug.WriteLine("error");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要进行实际的解析,您可以使用 json2.js 或 JQuery 的 $.parseJSON() 方法。这些将创建一个与您发送的 JSON 直接相似的 JavaScript 对象。
由于 Javascript 是一种脚本语言,您将不再考虑“多态性”,但您应该能够评估对象的属性(而不关心它们是什么“类型”),如下所示:
To do the actual parsing, you can use json2.js or JQuery's $.parseJSON() method. Those will create a javascript object that directly resembles the JSON you sent across.
Since Javascript is a script language, you won't be thinking in terms of "polymorphism" anymore, but you should be able to evaluate properties on the objects (without caring what "type" of object they are) like so:
尝试 https://github.com/douglascrockford/JSON-js/blob/ master/json2.js。有一个解析函数可以安全地将你的字符串解析为 JavaScript 对象。
Try https://github.com/douglascrockford/JSON-js/blob/master/json2.js. There is a parse function which will parse your string into a javascript object safely.