Convert C#.net object into json using js - javascript

I'm trying to convert a C#.net object into json on the client side.
I'm not sure that this is even possible...
I know that the object has been serializable with BinaryFormatter class.
Is it possible ? or i'm wasting my time on it.

Related

Creating Binary Data Structures in Javascript

I'm building a web app, and need to be able to encode user-generated data as concisely as possible before transmitting it to my server. In the past I've used Flash, and it had a very neat system where for any class that you want to serialize, you could write a pair of functions that would describe exactly how to serialize the data. For example:
out.writeShort(session);
out.writeUnsignedInt(itemID);
out.writeObject(arbitraryData);
out.writeShort(score);
You would have to write an equivalent function to read bytes from the serialized data and build the class from it.
Once data is serialized it could be encoded into a Base64 string for safe network transmission to the server.
I can't figure out how to do this in Javascript? JSON is nice and easy but it's incredibly wasteful, sending all object key/value pairs, and unless I'm mistaken everything is encoded as a string? So the value false is encoded as the string "false"?
Any advice on how to implement this in Javascript would be greatly appreciated! Use of libraries is fine so long as they work both on Node and in browser.
Look at this answer. You can use BSON format (Binary JSON) and it doesn't have those features of JSON you mentioned.

Find the structure of very long JSON format

I am fairly new to using JSON form, but need a specific part of the parsed JSON for my project. I was going to iterate through the proper object using dot notation. However the JSON I am getting from the API is EXTREMELY long (almost 60 pages) and I do not want to have to go through and trace the structure of it.
Is there anyway to compress the structure or figure this out programmatically?
I am using Node js if that makes any difference in this instance.

truncate string in mvc homecontroller javascript

I have a string coming in from the database say, ExpectedDate. I get the string in as 2015-07-08T00:00:00. I want to be able to remove the T00:00:00.from the string. Is this possible in MVC from the homecontroller javascript? if so, how?
I've tried to use a replace function, but I can't find the replace method in the MVC javascript file. What am I doing wrong?
Please help!
Controllers are not generally written in JavaScript in .NET. If you're going to do it in JavaScript client side, it would be much easier to load it into a Date object and then use the toDateString method with a format:
var d = new Date("2015-07-08T00:00:00")
d.toDateString("yyyy-MM-dd");
If you have the .NET DateTime object before it writes to the client, then you can do
your_date_time_object.ToString("yyyy-MM-dd");

JSON file parsing issue due to javascript variables

I've been provided with a json file to show results in a tableview.
But when I tried json file validation using online json file editor then I am getting "Build tree failed" error.
I posted this message to my client about wrong json file and then he has posted some strange description which I am totally unaware.
Yes the json is not a pure json. It has javascript variables which have json variable. For now u can use a webview to evaluate the javascript variables and use the json value. For ex : dp_f : corresponds to list of departure train is a javascript array variable. U may have to eval the variable and store each array element is a new json with key as dp_f and value of array of these values
Please tell me how can I use this json file which is not pure. What is the way to parse such files ?
The only real way to evaluate JavaScript (which is really what you have, not JSON) is to evaluate JavaScript.
You will have to use some form of eval(). I recommend trying a JavaScript sandbox, if available to you. Of course the best option would be to get the data fixed.

Working with identifiers as arrays and sending to MVC controller

I have some identifiers on my page. Named id[0], id[1] all the way to id[20] and sometimes more. I need to send these to an MVC controller and I would like to package them up as one object and unpack at the controller. Can someone tell me if this is possible, my knowledge of javascript is just basic so I'd really appreciate advice on which way to go. For example can I use JSON or serialize. btw I'm using jQuery.
Gordon
You can send your data in JSON.
See > ASP.NET MVC How to pass JSON object from View to Controller as Parameter
If you already have the id array (id[0], id1 , ... id[20]) you can convert it to JSON string with respect of JSON.stringify(id) where JSON.stringify function defined in the json2.js.
On the server side you can use for example Deserialize method of the JavaScriptSerializer to convert the data to List<T> where T is type of id[i] (for example string).
How to send data with respect of jQuery.ajax you already know from another answer.

Categories

Resources