Home > OS >  Convert a javascript object to c#
Convert a javascript object to c#

Time:07-25

Javascript

const information = {
        inventory: {
            backpack: {
                cords: [12, 20, 30, 50],
                size: [8, 30]
            },
            rood: {
                cords: [12, 20, 30, 50],
                size: [8, 30]
            },
            primary: {
                cords: [12, 20, 30, 50],
                size: [8, 30]
            }
        },
        game: {
            heal: {
                cords: [12, 20, 30, 50],
                size: [8, 30]
            },
            cooldown: {
                cords: [12, 20, 30, 50],
                size: [8, 30]
            }
        },
        menu: {
            exit: {
                cords: [12, 20, 30, 50],
                size: [8, 30]
            },
            pause: {
                cords: [12, 20, 30, 50],
                size: [8, 30]
            }
        }
    };

I want to convert this object to c# because I would like to get the information in this way but in c#.

var b_cords = information.inventory.backpack.cords;

C#

The idea is that the values already exist, I don't want to add then each one with its values, I have thought about using Json, that is to say to transform the object from JS to a Json and thus use it in C#, but it doesn't convince me because I would like the data to be of type System.Drawing.Rectangle.

var d = information.inventory.backpack.cords;

In the following way I can get the data as I want but I am not convinced, and I do not think it is the most orderly or optimized as possible, if someone can help me find a way to do something similar either using json or another method but that is better.

public class information
    {
        internal class inventory
        {
            internal class backpack
            {
                internal static Rectangle cords = Rectangle.Empty;
                internal static Size size = Size.Empty;
            }
            internal class rood
            {
                internal static Rectangle cords = Rectangle.Empty;
                internal static Size size = Size.Empty;
            }
            internal class primary
            {
                internal static Rectangle cords = Rectangle.Empty;
                internal static Size size = Size.Empty;
            }
        }

        internal class game
        {
            internal class heal
            {
                internal static Rectangle cords = Rectangle.Empty;
                internal static Size size = Size.Empty;
            }
            internal class cooldown
            {
                internal static Rectangle cords = Rectangle.Empty;
                internal static Size size = Size.Empty;
            }
        }
        
        internal class menu
        {
            internal class exit
            {
                internal static Rectangle cords = Rectangle.Empty;
                internal static Size size = Size.Empty;
            }
            internal class pause
            {
                internal static Rectangle cords = Rectangle.Empty;
                internal static Size size = Size.Empty;
            }
        }
    }

CodePudding user response:

I assume you're fairly new to the world of JS/C#, objects, and JSON?

JSON stands for JavaScript Object Notation. It's a condensed way of describing data and it looks shockingly like JS objects that you've shown in your question already.

The only difference is that it's a string, because we can send strings places but not objects.

JS/TS and C# all have serialisers/deserialisers to take objects and turn them into strings - and the reverse.

JS object:

const myObj = {
  someProperty: 123,
  anotherProp: 'hello'
};

Run that object through JSON.stringify(myObj) and you get the string:

const myStr = JSON.stringify(myObj);

// myStr ==
// {
//   "someProperty": 123,
//   "anotherProp": "hello"
// }

Formatted for your convenience.

If you were to give that string back to the JS, you can run it through JSON.parse(myStr) and get your object back.

const myObjAgain = JSON.parse(myStr);

The C# version of this (one of them) is JsonConvert.SerializeObject(myObj) and JsonConvert.DeserializeObject<MyObjType>(myStr).

The serialisers for both are very efficient, and JSON itself is, I think, industry standard these days for passing data between clients and servers.

... Assuming you're doing such a thing.

It's also frequently used with NoSQL/DocumentDB type data bases, all the data stored is often JSON format.

I suspect most things - I can only vouch for C# and Angular - have JSON (de)serialisation baked into them.

The Angular HttpClient deals directly with JSON data by default, and C# controllers/actions will handle data binding with JSON payloads out of the box as well.

C#, being typed, and per my example above, has JsonConvert.DeserializeObject<TYPE>(...), where you tell the serialiser what type you expect that data to be.

In your case, I guess it would be JsonConvert.DeserializeObject<Information>(informationJsonString).

Assuming you created matching C# classes...

public class Information {
    public Inventory Inventory { get; set; }
    ...
}

I'll finish by echoing the comments: very few things need to be static. Don't default to doing that.

Also, follow standards/conventions for the language you're using. Look up the relevant style guides.

Programming is what we do, but we communicate and work cohesively in our teams by using the same language - not programming language, but that of consistency, standards, and patterns that we all recognise and adhere to.

  •  Tags:  
  • c#
  • Related