JSON string-to-JSON object conversion

Source: Internet
Author: User
Tags javascript eval

JSON (JavaScript object Notation) JS objects symbol

is a lightweight format for data interchange

JavaScript eval () function implementation

(i) standard format

function jsonformatting () {    var jsonstring = '{' Unid ': ' 1 ', ' CustomerName ': ' Song Jiang ', ' age ':' ""} ';    var jsonobject = eval ('('jsonstring') ');    var tt = ';    $.each (Jsonobject,function (k, v) {        tt + = k + ":" + v+ "<br/>";    });    $ ("#divmessage"). HTML (TT); }

The notation is that the key value is surrounded by double quotes. This format can be called a json string. converts a JSON string to a JSON object through the Eval function .

(ii) abbreviated format

function jsonformatting () {    var jsonstring = '{Unid: ' 1 ', CustomerName: ' Song Jiang ', Age:' "} ';    var jsonobject = eval ('('jsonstring') ');    var tt = ';    $.each (Jsonobject,function (k, v) {        tt + = k + ":" + v+ "<br/>";    });    $ ("#divmessage"). HTML (TT); }

The key value omits the quotation marks and converts the JSON string into a JSON object through the Eval function, which can then be accessed in two ways:

@1. Dot Syntax object.property

var jsonstring = ' {' Unid ': ' 1 ', ' CustomerName ': ' Song Jiang ', ' Age ': ' ""} '; var jsonobject = eval (' (' + jsonstring + ') '); Alert (  Jsonobject.customername);

@2. Index of key-value pairs

Object[key]

var jsonstring = ' {' Unid ': ' 1 ', ' CustomerName ': ' Song Jiang ', ' Age ': ' ""} '; var jsonobject = eval (' (' + jsonstring + ') '); Alert ( jsonobject["CustomerName"]);

eval function Description:

It can execute a string as if it were a JavaScript expression.

The Eval function receives a parameter s, and if s is not a string, it returns s directly. Otherwise, the S statement is executed.

If the S statement execution result is a value, this value is returned, otherwise undefined is returned. Object declaration syntax '{} ' does not return a value , it needs to be enclosed in parentheses to return a value.

function Evaltest () {    var code1 = ' A ' + 2 ';    Expression    var Code2 = ' {a:2} ';      Statement    alert (eval (code1));     Output A2    alert (eval (code2));     Output undefined    alert (eval (' (' + Code2 + ') ');//Output [Object Object]}

 

For an object declaration statement, it is only execution and cannot return a value.

In order to return an object declaration statement such as a commonly used "{}", it must be enclosed in parentheses to convert it to an expression in order to return its value.

Adds a conversion method from JSON strings to JSON objects for jquery.

Open the library and locate Jquery.extend ({

Add method

Jsontoobject:function (a) {     return eval (' (' + A + ') ');     

Use the following:

var jsonstring = ' {' Unid ': ' 1 ', ' CustomerName ': ' Song Jiang ', ' Age ': ' ""} '; var jsonobject = $.jsontoobject (jsonstring); alert ( jsonobject["CustomerName"]);

Where jsonstring is a json string, and Jsonobject is a JSON object

jquery implementation

$.parsejson ("Jsonstr"); Convert a JSON string to a JSON object

Using the Json.js Package plugin

var obj = json.parse (Strjson);

JSON object to JSON string

var str = json.stringify (obj) needs to import Json.js package

function jsonformatting () {            var jsonobject = {"Unid": "1", "CustomerName": "Song Jiang", "Age": "$"};                        var jsstr=json.stringify (jsonobject);            Alert (typeof jsstr);                               }

  

JSON string-to-JSON object conversion

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.