1

In C# I have used NewtonSoft.JsonConvert.DeserializeObject to deserialise a complex JSON object into custom C# Objects. The JSON is actually carved up into different objects, its not a 1-1 relationship.

Is there an equivalent way to do this with Javascript?

The examples I've seen appear simplistic like:

function Foo(a, b)
{
   this.a = a;
   this.b = b;
}

Is there a better way than doing something like this?

function Foo(a, b)
{
   this.a = a;
   this.b = b;
   this.loadFromJson = function() {code...};
   this.saveToJson = function() {code...};
}

Edit: I know I can use json.parse functionality to load the JSON data into an object but that’s a 1:1 relationship.

The c# code I am converting loads a complex json payload from a REST endpoint then deserializes that into different classes which are effectively different views of the data.

ServerMonkey
  • 1,042
  • 3
  • 19
  • 42

0 Answers0