简体   繁体   English

使用JavaScript保存和检索XML和JSON

[英]Saving and retrieving XML and JSON using JavaScript

I'm having trouble understanding how to read and write XML and JSON data using JavaScript. 我在理解如何使用JavaScript读写XML和JSON数据时遇到了麻烦。 Can anyone provide me detailed explanation with examples. 任何人都可以提供示例的详细解释。

 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#b01").click(function(){ htmlobj=$.ajax({url:"/jquery/test1.txt",async:false}); $("#myDiv").html(htmlobj.responseText); }); }); </script> </head> <body> <div id="myDiv"><h2>this is a demo</h2></div> <button id="b01" type="button">change</button> </body> </html> 

you can use ajax. 您可以使用ajax。

Lets assume you receive the following JSON from a web server via an API call or some form of Scraping method 假设您通过API调用或某种形式的Scraping方法从Web服务器接收以下JSON

'{ "firstName":"Foo", "lastName":"Bar" }'

To access this data you first need to parse the raw JSON and form a Javascript object 要访问此数据,您首先需要解析原始JSON并形成一个Javascript对象

let response = JSON.parse('{ "firstName":"Foo", "lastName":"Bar" }');

This forms an object which we can access relativly simply 这形成了一个我们可以相对简单地访问的对象

let firstName = response["firstName"];
let lastName = response["lastName"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM