我一直对何时使用这两种解析方法感到困惑。
在我回显json_encoded数据并通过ajax检索它之后,我经常对何时应该使用JSON感到困惑。stringify和JSON.parse。
我得到[对象,对象]在我的控制台.log解析和JavaScript对象时stringized。
$.ajax({
url: "demo_test.txt",
success: function(data) {
console.log(JSON.stringify(data))
/* OR */
console.log(JSON.parse(data))
//this is what I am unsure about?
}
});
JSON。stringify将JavaScript对象转换为JSON文本,并将该JSON文本存储在字符串中,例如:
var my_object = { key_1: "some text", key_2: true, key_3: 5 };
var object_as_string = JSON.stringify(my_object);
// "{"key_1":"some text","key_2":true,"key_3":5}"
typeof(object_as_string);
// "string"
JSON。parse将JSON文本字符串转换为JavaScript对象,例如:
var object_as_string_as_object = JSON.parse(object_as_string);
// {key_1: "some text", key_2: true, key_3: 5}
typeof(object_as_string_as_object);
// "object"
首先,JSON.stringify()函数将JavaScript值转换为JavaScript对象符号(JSON)字符串。JSON.parse()函数将JavaScript对象表示法(JSON)字符串转换为对象。有关这两个功能的更多信息,请参阅以下链接。
https://msdn.microsoft.com/library/cc836459 (v = vs.94) . aspx
https://msdn.microsoft.com/library/cc836466 (v = vs.94) . aspx
其次,下面的示例将有助于您理解这两个函数。
<form id="form1" runat="server">
<div>
<div id="result"></div>
</div>
</form>
<script>
$(function () {
//define a json object
var employee = { "name": "John Johnson", "street": "Oslo West 16", "phone": "555 1234567" };
//use JSON.stringify to convert it to json string
var jsonstring = JSON.stringify(employee);
$("#result").append('<p>json string: ' + jsonstring + '</p>');
//convert json string to json object using JSON.parse function
var jsonobject = JSON.parse(jsonstring);
var info = '<ul><li>Name:' + jsonobject.name + '</li><li>Street:' + jsonobject.street + '</li><li>Phone:' + jsonobject.phone + '</li></ul>';
$("#result").append('<p>json object:</p>');
$("#result").append(info);
});
</script>
JSON:主要用于与服务器交换数据。在发送之前
JSON对象给服务器,它必须是一个字符串。
JSON.stringify() //Converts the JSON object into the string representation.
var jsonData={"Name":"ABC","Dept":"Software"};// It is a JSON object
var jsonString=JSON.stringify(jsonData);// It is a string representation of the object
// jsonString === '{"Name":"ABC","Dept":"Software"}'; is true
它还将Javascript数组转换为字符串
var arrayObject=["ABC","Software"];// It is array object
var arrString=JSON.stringify(array);// It is string representation of the array (object)
// arrString === '["ABC","Software"]'; is true
当我们从服务器接收到JSON数据时,数据将是字符串格式。因此,我们将字符串转换为JSON对象。
JSON.parse() //To convert the string into JSON object.
var data='{ "name":"ABC", "Dept":"Software"}'// it is a string (even though it looks like an object)
var JsonData= JSON.parse(data);// It is a JSON Object representation of the string.
// JsonData === { "name":"ABC", "Dept":"Software"}; is true
var log = { "page": window.location.href,
"item": "item",
"action": "action" };
log = JSON.stringify(log);
console.log(log);
console.log(JSON.parse(log));
//输出将是:
//对于第一个控制台是一个字符串:
'{ "page": window.location.href,"item": "item","action": "action" }'
//第二个控制台是一个类似对象:
Object {
page : window.location.href,
item : "item",
action : "action" }
JSON.parse()用于将String转换为Object。
JSON.stringify()用于将Object转换为String。
你也可以参考这个…
<script type="text/javascript">
function ajax_get_json(){
var hr = new XMLHttpRequest();
hr.open("GET", "JSON/mylist.json", true);
hr.setRequestHeader("Content-type", "application/json",true);
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
/* var return_data = hr.responseText; */
var data=JSON.parse(hr.responseText);
var status=document.getElementById("status");
status.innerHTML = "";
/* status.innerHTML=data.u1.country; */
for(var obj in data)
{
status.innerHTML+=data[obj].uname+" is in "+data[obj].country+"<br/>";
}
}
}
hr.send(null);
status.innerHTML = "requesting...";
}
</script>