JS read JSON data

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Examples are as follows:

复制代码

<script type="text/javascript" language="javascript">

 var languages = {

cn:{

lang:'ch',

name:"Chinese"

 },

 en:{

lang:"English",

name:"en"

 }

 };

 document.write(languages.cn.name);

</script>

复制代码


=====================================================================

 Do you have any examples of methods with JS read JSON

//According to the city name

function getNameByCity(city)

{

   var cityName =

   {

"0556":"The city of Anqing",

"0372":"The city of Anyang",

   };

   return cityName[city];

}


 =====================================================================

To explore the method of reading JSON in JS

Methods JS read JSON I come into contact with a two:

Method: the return of function structure definition method

var strJSON = "{name:'json name'}";//The obtained JSON

var obj = new Function("return" + strJSON)();//The converted JSON object

alert(obj.name);//json name

Method two: well-known Eval function in JS

var strJSON = "{name:'json name'}";//The obtained JSON

var obj = eval( "(" + strJSON + ")" );//The converted JSON object

alert(obj.name);//json name

Need to pay attention to the second kind of method is, the expression {'name''json name'} object: &ldquo must be used; (&rdquo); extended live, otherwise

var strJSON = "{name:'json name'}";

var obj = eval(strJSON);

alert(obj.constructor);//The String constructor

alert(obj.name);//undefine

Must take the object expression expanding Eval execution can generate an anonymous object!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Reuben at November 15, 2013 - 11:53 PM