How to parse Json.NET polymorphic objects?

前端 未结 2 711
伪装坚强ぢ
伪装坚强ぢ 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:03

    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.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
提交回复
热议问题