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.
Note: Logging and debugging is currently turned off for this example.
All YUI 2.x users should review the YUI 2.8.2 security bulletin, which discusses a vulnerability present in YUI 2.4.0-2.8.1.