Mutual conversion between json object and string object in JavaScript

JSON: JavaScript Object Notation (JavaScript Object Notation), in fact, JSON is just a JavaScript object (Object).

json object 

code show as below:

var json = {aa:true,bb:true}; 
var json1 = {aa:'b',bb:{cc:true,dd:true}}; 

1: js manipulates json objects 

code show as below:

for(var item in json){ 
alert(item); //结果是 aa,bb, 类型是 string 
alert(typeof(item)); 
alert(eval("json."+item)); //结果是true,true类型是boolean 
eval(("json."+item+"=false;")); //改变json对象的值 
} 

2: The method of converting json object to String object 

code show as below:

/** 
* json对象转字符串形式 
*/ 
function json2str(o) { 
var arr = []; 
var fmt = function(s) { 
if (typeof s == 'object' && s != null) return json2str(s); 
return /^(string|number)$/.test(typeof s) ? "'" + s + "'" : s; 
} 
for (var i in o) arr.push("'" + i + "':" + fmt(o[i])); 
return '{' + arr.join(',') + '}'; 
} 

3: string object is converted to json object 

code show as below:

function stringToJson(stringValue) 
{ 
eval("var theJsonValue = "+stringValue); 
return theJsonValue; 
} 

4: The method of converting the json array into a String object (if you want to drop the above method) 

code show as below:

function JsonArrayToStringCfz(jsonArray) 
var JsonArrayString = "["; 
for(var i=0;i<jsonArray.length;i++){ 
JsonArrayString=JsonArrayString+JsonToStringCfz(jsonArray[i])+","; 
} 
JsonArrayString = JsonArrayString.substring(0,JsonArrayString.length-1)+"]"; 
return JsonArrayString; 
} 

5: Use json.js json to string 

code show as below:

<script src="json2.js"></script> 
<script> 
var date = {myArr : ["a" , "b" , "c" , "d"] , count : 4}; 
var str = JSON.stringify(date); 
alert(str); 
</script> 

Create a new string (JSON text) in javascript.

var txt = '{ "employees" : [' +
'{ "firstName":"Bill" , "lastName":"Gates" },' +
'{ "firstName":"George" , "lastName":"Bush" },' +
'{ "firstName":"Thomas" , "lastName":"Carter" } ]}'; 

Since JSON syntax is a subset of JavaScript syntax, the JavaScript function eval() can be used to convert JSON text into JavaScript objects.

The eval() function uses the JavaScript compiler, which parses the JSON text and produces a JavaScript object. The text must be enclosed in parentheses to avoid syntax errors:

var obj = eval ("(" + txt + ")"); 

Note: The eval() function compiles and executes any JavaScript code. This hides a potential security issue.

It is safer to use a JSON parser to convert JSON to JavaScript objects. The JSON parser only understands JSON text, it doesn't compile scripts.

In browsers, this provides native JSON support and a faster JSON parser.

Newer browsers and the latest ECMAScript (JavaScript) standard include native support for JSON.

String to json object: JSON.parse(jsonstr);

json object to json string: JSON.stringify(jsonObj); 

JQuery method:

json string to json object: jQuery.parseJSON(jsonStr);

<html>
<body>
<h2>通过 JSON 字符串来创建对象</h3>
<p>
First Name: <span id="fname"></span><br /> 
Last Name: <span id="lname"></span><br /> 
</p> 
<script type="text/javascript">
var txt = '{"employees":[' +
'{"firstName":"Bill","lastName":"Gates" },' +
'{"firstName":"George","lastName":"Bush" },' +
'{"firstName":"Thomas","lastName":"Carter" }]}';
obj = JSON.parse(txt);
document.getElementById("fname").innerHTML=obj.employees[1].firstName 
document.getElementById("lname").innerHTML=obj.employees[1].lastName 
</script>
</body>
</html> 

So how to iterate through the json array? It can be treated as a normal javascript object.

<html>
<body>
<h2>如何遍历JSON数组</h3>
<div id="result"></div>
<script type="text/javascript">
var txt = '[{"firstName":"Bill","lastName":"Gates" },' +
'{"firstName":"George","lastName":"Bush" },' +
'{"firstName":"Thomas","lastName":"Carter" }]';
var arrayJson = JSON.parse(txt);
var html='';
for(var p in arrayJson){
html+=' firstName:'+arrayJson[p].firstName;
html+=' lastName'+arrayJson[p].lastName;
html+='<br />';
}
document.getElementById("result").innerHTML= html;
</script>
</body>
</html> 

ie8 (compatibility mode), ie7 and ie6 do not have JSON objects, but http://www.json.org/js.html provides a json.js, so that ie8 (compatibility mode), ie7 and ie6 can support JSON objects and Its stringify() and parse() methods; you can get this js at https://github.com/douglascrockford/JSON-js, generally use json2.js now.

ie8 (compatibility mode), ie7 and ie6 can use eval() to convert strings to JSON objects,

var c='{"name":"Mike","sex":"女","age":"29"}';
var cToObj=eval("("+c+")");
alert(typeof(cToObj)); 

Guess you like

Origin blog.csdn.net/cdming/article/details/130314119