How to build a JavaScript object from a JSON schema by default?

I started using Newtonsoft.Json.Schema.JsonSchemaGenerator along with various property attributes in my C # code to support my DRY script client. What I would like to do is create the client part of the initialized default object based on the scheme from the server. This would be useful, for example, when the user clicks "Create Foo" to add a new record to the table.

Obviously, I can simply program it to repeat the .Properties parameters and create a new object, which I am doing at the moment. However, I would prefer not to invent new wheels.

Are there any JS libraries for working with a JSON scheme that will do this, among other great things that I still have to understand?


1/29/2013 UPDATE

Some people tried to answer my question and were out of the database, and as a result received some negative reviews from the SO community. So let me try to clarify the situation. Here is the challenge:

  • In the JS client script, you have an object that represents the JSON Schema of another object. Let's say this happened from the server through JSON.NET and is a C # class representation.

  • Now, in the JS client script, create one of these objects based on the JSON Schema . Each property / property in the object must be initialized by default in accordance with the scheme, including all contained objects!

  • BONUS: bind this new object to the user interface using MVVM (e.g. Knockout). Change some fields in response to user input.

  • Send this new object to the server. Server code will add it to the collection, database table, whatever. (Yes, the object will be sent as JSON using Ajax - we can assume that)

  • No duplication ! The only place the class is defined is server-side code (C # in my example). This includes all metadata, such as default values, description text, valid ranges, etc.

+7
source share
2 answers

I think ... you should use two-way binding to your HTML code ... therefore, as soon as your client side changes, you will end up in your costume js file.

check here for js knockout.

Get JS link

and in C # use the code: $ ("# urlhidden"). val () OR Document.GetElemenyByID ("# urlhidden"). val ().

here you will get the value of the array / list or text field

Use json with Ko

create a new view model for js knockout, which you will learn about in the link above.

and create a json call like:

self.LoadMAS_Client = function () { try { var params = { "clientID": ClientId }; $.ajax({ type: "POST", url: "http://" + ServerString + "/Services/LogisticsAppSuite-Services-Web-Services-MasClientService.svc/Json/GetAllLevelSubClients", contentType: 'application/json', data: JSON.stringify(params), dataType: 'json', async: false, cache: false, success: function (response) { // in response u will get the data.and use as per your requirement. eg. self.SelectedClient(response.your value); }, error: function (ErrorResponse) { } }); } catch (error) { } }; 

=============================== New update ============== ===== ========================= I think ... how can you do this ... get the data in xml format in C # code and is hidden in a string json ... check below code // Convert XML node contained in xml string to JSON string

 XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); string jsonText = JsonConvert.SerializeXmlNode(doc); // To convert JSON text contained in string json into an XML node XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json); 
-one
source

Yes there is (I tried this with NodeJS):

JSON default settings

-one
source

All Articles