How to parse Json.NET polymorphic objects?

前端 未结 2 755
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 06:30

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 worki

2条回答
  •  遇见更好的自我
    2021-01-21 07:06

    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:

    var obj = $.parseJSON(json);
    var firstAnimalName = obj.Animals[0].Name;
    

提交回复
热议问题