objects and arrays-converting data to Objects

Source: Internet
Author: User

————————————————————————————————————

<script type= "Text/javascript" >
Create the data for the object based on the user's input
function Buildobj () {
Get new elements for the DOM of key and Val
var key = document.getElementById (' key '). Value;
var val = document.getElementById (' val '). Value;
Gets the contents of the current object text
var str = document.getElementById (' Obj_txt '). Innerhtml.trim ();
Convert a character to a JavaScript object
var obj = str== '? {}:eval (' (' +str+ ') ');
Use regular expressions, if the data is numeric, connect directly, otherwise you need to add single quotation marks
if (/\d+\.? \d*/.test (Val))
Obj[key] = val;
Else
Obj[key] = ' \ ' +val+ ' \ '; Add single quotes to Val
Print out the text of the latest object
document.getElementById (' Obj_txt '). InnerHTML = ConvertObj (obj);
}
Convert objects into textual form, which is also the form of JSON conversion
function ConvertObj (obj) {
var str = ' {'; Defines a character variable for concatenation
For (var prop in obj) {//properties of the convenience object
If it is an object type, the connection is traversed
if (typeof (Obj[prop]) = = = ' object ') {
str + = prop+ ': ' +convertobj (Obj[prop]) + ', ';
If it is in character form, you need to add single quotation marks
}else if (typeof (Obj[prop]) = = ' String ') {
str + = prop+ ': \ ' +obj[prop]+ ' \ ', ';
The others are displayed directly
}else{
str + = prop+ ': ' +obj[prop]+ ', ';
}
}
Remove the last comma, otherwise it is not a standard form
if (Str.charat (str.length-1) = = ', ') {
str = STR.SUBSTR (0,str.length-1);
}
str + = '} '; Stitching the last curly brace
return str; Returns the final stitching result character
}
</script>

————————————————————————————————————

The function of the convetobj () function is to convert data from character form to object form, which also uses recursive methods.

In this function, you need to first consider the type of data, the basic data type is only numeric and character type, the number type does not have to add single quotation marks, and the character type needs to add single quotation marks.

objects and arrays-converting data to Objects

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.