JSON string converted to JSON object, JSON object converted to string, value converted to string, string to value

Source: Internet
Author: User

Main content:

I. JSON-related concepts

Second, JSON string converted to JSON object (String-->json object)

The JSON object is converted to a string (JSON object-to-string)

Iv. converting a value to a string (value-to-string)

V. String to value (string-to-value)

Synchronized Exchange Learning Community: http://www.mwcxs.top/page/425.html

I. JSON-related concepts

JSON, all called JavaScript Object notation, is a lightweight form of data interaction. A completely language-independent text format is an ideal data interchange format.

At the same time, JSON is the native format of JavaScript, so JavaScript operations that handle JSON do not require any packages, APIs, any dependencies.

There are two structures in JSON: (1) Array (2) object

(1) What is an array

The array is separated by the value "," (comma), which ends with "[" and "]".

Like what:

[{        "key": "Test1",        "value": 123,        "type": "Number",        "required" : "True",        "description": "Field description 2",        "Children": []    }, {        "key": " Test2 ",        " value ": 345,        " type ":" Boolean ",        " required ":" True ",         "description": "Field Description 4",        "Children": []    }]

Children is an array with two values in the array, and the value is an object.

(2) What is an object

The object is to start with "{", Ending with "}", followed by each pair of key values, separated by a colon between the key and the value. If the value is a character, it must be in quotation marks, and the numeric type is not required.

{        "key": "Test1",        "value": 123,        "type": "Number",        "required": "True",        "description": "Field description 2",        "Children": []    }

In data transmission, JSON is passed in the form of text, which is a string, while JS operates a JSON object, so the conversion between the JSON object and the JSON string is critical.

(3) string and JSON object chestnut

JSON string

var str1 = ' {        ' key ': ' Test1 ',        ' value ': 123,        ' type ': ' Number ',        ' Required ":" True ",        " description ":" Field description 2 ",        " Children ": []    }' 

is to add ', ' on the basis of the object.

JSON object:

var str2 = {        "key": "Test1",        "value": 123,        "type": "Number",        " Required ":" True ",        " description ":" Field description 2 ",        " Children ": []    }

Second, JSON string converted to JSON object (String-->json object)

There are three types of methods : conversion function, JQ conversion function, eval () method

Method: Json.parse () method of the conversion function

var a = ' {' A ': 2} '; Json.parse (a);  {A:2}

Note: IE8 (compatibility mode) OK, but IE6 and IE7 do not have JSON objects and require additional json.js or json2.js.

Method 2:parsejson () method to convert a string to a non-string

With the jquery plugin.

// Jquery.parsejson (JSONSTR), you can convert a JSON string into a JSON object

In turn, use the Serialize series method: for example: var fields = $ ("SELECT,: Radio"). Serializearray ();

Method 3:eval () method

The eval () function computes a string and executes the JavaScript code in it.

var s = ' {a:2} '; eval (' (' + S + ') '); To turn the JSON string into a JSON object, wrap a pair of parentheses () outside the string,   {a:2}

Note: IE8 (compatibility mode), IE7 and IE6 can also use the eval () method to convert a string to a JSON object, but it is not recommended because it is unsafe to perform an expression of the pick string.

The value of a in the figure corresponds to the expression 2+4, and when the eval () method is executed, the value of the expression has been computed, with a value of 6.

The JSON object is converted to a string (JSON object-to-string)

Method--Conversion function: Json.stringify () method to convert a non-string into a string.

var s = {' A ': 2}; Json.stringify (s);  "{" A ": 2}"

Iv. converting a value to a string (value-to-string)

There are three kinds of methods : Conversion function tostring (), weakly type conversion, coercion type conversion

Method: Value.tostring () converts a value into a string corresponding to the binary

var n =8; n.tostring (binary);

Note: You cannot convert null and undefined to a string

Method 2--Weak-type conversions: Value + ' converts values to strings


Note: Using the addition algorithm, with an empty string, the disadvantage is that the readability is poor

Method 3--Coercion Type conversion: String (value)

String (2)

V. String to value (string-to-value)

There are three kinds of methods: conversion function, coercion type conversion, JS variable weak type conversion

The value obtained when JS reads a text box or other form data is of type string.

Method of the conversion function: parseint () and parsefloat ()

parseint ("1234blue");   // returns 1234parseint ("22.5");     // returnsparseint ("Blue");    // returns NaN
// returns 175 // returns 2
Parsefloat ("1234blue");   // returns 1234.0parsefloat ("22.5");      // returns 22.5parsefloat ("Blue");     // returns NaN

Method 2--Coercion Type conversion

Number (value)-converts the given value to a digit (which can be an integer or a floating point);

Number (false) 0number (true) 1 number(undefined) nannumber (null) 0  "5.5") 5.5"5.6.7") NaN

Method 3--js Weak type conversion

var str= ' 012.345 '; var x = str-0;       Output 12.345

First look at the above example, only the arithmetic operation, the implementation of the string to the number of the type conversion , but this method is not recommended;

PS: Rounding of decimals

1. Rounding

var num =2.4492425542;   //

2, not rounding

To turn a decimal number into an integer

Math.floor (15.7784514000 *)/+/   / output  is 15.77

Use regular matching as a string

Number (15.7784514000.toString (). Match (/^\d+ (?: \. \d{0,2}))    //  output result is 15.77, cannot be used for integers such as 10 must be written as  10.0000

Note: If it is a negative number, convert it to a positive number and then the negative

JSON string converted to JSON object, JSON object converted to string, value converted to string, string to value

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.