Typical method for generating JSON metadata objects

Source: Internet
Author: User

How do you feel if you only need to reference a JavaScript variable in the Condition Statement and alarm instead of the server code? Does this code look much better? It does not mix server code into JavaScript. If the server changes the method for storing metadata, you do not need to rewrite the client script. This makes the days of JSON objects easier.

  • The author provides detailed instructions on the Json getting started document.
  • A full description of the JSON history
  • Comparison between JSON and XML
  • Details about the number of elements in JSON format that the server code will generate
  • Detailed descriptions of server-side JSON Performance

You will use a JavaScript Object to group our verification data and messages to a level. Then access these messages just like accessing a JavaScript object at the access level. You have done that! When this JSON metadata object is ready, the previous JavaScript code snippets will be similar.

 
 
  1. if(cc < vehicleValidationsMetadata.CC.minCC ||   
  2.                      cc > vehicleValidationsMetadata.CC.maxCC) {  
  3.      alert(vehicleValidationsMetadata.CC.RangeMessage);  
  4.      } 

Now, the question is who will prepare the JSON metadata object? Well, only servers can do this job. The server must generate this JSON object and provide it to the client browser ). Some Java APIs can help you prepare JSON objects of any type. See references to view those APIs.

A typical method for generating JSON metadata objects is:

 
 
  1. var vehicleValidationsMetadata = {  
  2.      "BrandName":{  
  3.                    "CanContainDigits":{false},  
  4.                "MaxWords":{2},  
  5.              "FormatMessage":{"Brand Name cannot contain digits."},   
  6.            "WordLimitMessage":{"Brand Name cannot contain more than two words"}  
  7.          }, "RegistrationNumber":{  
  8.              "CanContainAlphabets":{false},  
  9.               "CanContainDigits":{"true"},  
  10.                 "FormatMessage":{"Registration Number can contain only digits."}  
  11.      },  
  12.  "CC":{  
  13.               "minCC":{50},  
  14.                  "maxCC":{5000},  
  15.                 "FormatMessage":  
  16.              {"CC can only be numeric"},   
  17.                  "RangeMessage":{"CC can be within range of 50 and 5000"}  
  18.      }  
  19.  } 

Prepare a hierarchical Java object for the object and its verification message. ToString () is called for these entities and their verification messages (). These entities and their verification messages are most likely to provide you with a JSON string.
Store the string to another request range. In JSP, obtain the string and assign it to the braces of the JavaScript variable value.

The final metadata object of the vehicle.

 
 
  1. function validateVehicleData() {  
  2.       var brandName = //get brand name from form field  
  3.    var registrationNumber = //get Registration Number from form field.  
  4.         var CC = //get CC from form field  
  5.    var brandNamebrandNameTokens = brandName.split(' ');  
  6.         if(brandNameTokens.length > vehicleValidationsMetadata.BrandName.MaxWords) {  
  7.             alert(vehicleValidationMessages.BrandName.WordLimitMessage);  
  8.        }  
  9.   .  
  10.   .  
  11.   .  
  12.   if((!vehicleValidationsMetadata.RegistrationNumber.CanContainAlphabets) &&   
  13.                      isNaN(parseInt(registrationNumber))) {  
  14.  alert(vehicleValidationMessages.RegistrationNumber.FormatMessage);  
  15.  }  
  16.   var ccNum = parseInt(CC);  
  17.   if(ccNum < vehicleValidationMessages.CC.minCC ||   
  18.                      ccNum > vehicleValidationMessages.CC.maxCC) {  
  19.                alert(vehicleValidationMessages.CC.RangeMessage);  
  20.   }  

The server must generate the entire string, except the first and last lines, because the current user language environment may require the use of these messages and only the server code can do this ). Note that this metadata object is only used to verify vehicles. Ideally, the JSON object is encapsulated into the person metadata object.

In this way, you do not need to create another JSON object, but only need to include this metadata object into the person metadata object. After preparing the metadata object, you can use the metadata and messages in the object to verify data input and display messages. Now, the JavaScript function for Verifying vehicle input information looks like Listing 4.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.