This example shows how to use the reviver
parameter in JSON.parse
to add new object members and format existing members during the parsing phase.
Choose a currency, then get the data
SKU | Item | Price (USD) | Price (USD) |
---|---|---|---|
Click Get Data |
The data returned from the server will be a JSON string containing this object structure:
We'll contain all the moving parts in the YAHOO.demo.JSONReviver
namespace. In it, we'll include the currency exchange rates and a function to reference the rates to add a new member to the JSON response as it is being parsed.
When the Get Data button is clicked, we send an asyncRequest
for the JSON data from YUI's Connection Manager. In our success handler, we pass our conversion function to JSON.parse
with the response text. The resulting inventory records will have an additional member, convertedPrice
. This data is then passed to a UI method to update the inventory table.
Below is the full source for the example, including the functions responsible for formatting the price and updating the UI.
You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.
Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.
Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings