JSON data acceptance in JavaScript, processing introduction

Source: Internet
Author: User

JS Way to read JSON I have two kinds of contacts:

Method One: Function construction definition method returns

The code is as follows Copy Code

var Strjson = "{name: ' JSON name '}";//Get JSON
var obj = new Function ("return" + Strjson) ()//converted JSON object
alert (obj.name);//json name

Method Two: The famous eval function in JS

The code is as follows Copy Code

var Strjson = "{name: ' JSON name '}";//Get JSON
var obj = eval ("(+ Strjson +)");//Converted JSON object
alert (obj.name);//json name

The second way to be aware of this is that the object expression {' name ': ' JSON name '} must be extended with ' () ' otherwise

The code is as follows Copy Code

var Strjson = "{name: ' JSON name '}";
var obj = eval (Strjson);
alert (obj.constructor);//string constructor
alert (obj.name);//undefine

An object expression must be extended to eval execution to generate an anonymous object!

JS receives JSON data across domains

PHP Script Service side

The code is as follows Copy Code

<?php
if (Isset ($_get[' callback ')) &&!empty ($_get[' callback ')) {
Echo $_get[' callback ']. ' ({"name": "Snow Residual Love", "profession": "Phper"}) ';
}
?>

$_get[' Callback ' is the name of the JS function to be invoked on the client, {"name": "Snow Residue", "profession": "Phper"} is the JSON data returned by the server.

Four, JS client

The code is as follows Copy Code

<script type= "Text/javascript" >
function func (data) {
Alert (' Name: ' + data.name + ', Occupation: ' + data.profession ');
}
</script>

<script type= "Text/javascript" src= "/technology/jsonp/server.php?callback=func" ></script>

You can do it locally, the result will pop up "name: Snow Phper, Occupation:".

Five, jquery application

The Getjson function of jquery has integrated cross-domain access by following a parameter in the address of the URL, with a placeholder instead of the parameter value, as shown below, where jquery automatically replaces the placeholder with the function name of the timestamp at execution time.

The code is as follows Copy Code

<script type= "Text/javascript" src= "/js/jquery-1.3.2.min.js" ></script>
<script type= "Text/javascript" >
$.getjson ('/technology/jsonp/server.php?callback=? ', function (data) {
Alert (' Name: ' + data.name + ', Occupation: ' + data.profession ');
});
</script>

We need to process JSON data after acceptance.

The code is as follows Copy Code

function Showuserlist ()
{
Postxmlhttp ("./userlistservlet.do", "Usercallback (Resultvalue)", "Loading ()");
}

function Usercallback (jsondate)
{
JSON data serialized into a JS object
var jsobject = eval (' (' +jsondate+ ') ');
var htmlstr = "<table align=center width=60%>" +
"<tr>" +
"<th> user id</th><td> password </td><td> username </td><td> location </td><td> operation </td> "+
"</tr>";
for (Var i=0;i<jsobject.length;i++)
{
htmlstr+= "<tr id= ' tr" +i+ "' >" +
"<td>" +jsobject[i].userid+ "</td>" +
"<td>" +jsobject[i].pword+ "</td>" +
"<td>" +jsobject[i].username+ "</td>" +
"<td>" +jsobject[i].position+ "</td>" +
"<td><a herf= ' # ' onclick=" Edituser (tr "+i+"); " > Edit </a></td> "+
"</tr>";
}
div.innerhtml=htmlstr+ "</table>";
}

function Loading ()
{
Div.style= "With:100%;text-align:center";
Div.innerhtml= "<font color=red> is fetching data please wait ... ...</font>";

}

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.