Note that there are some explanatory texts on larger screens.

plurals
  1. POYUI JSON - How do I Add New Object Members During Parsing?
    primarykey
    data
    text
    <p>For nearly two days I have been stuck trying to get the YUI JSON Utility: Adding new object members during parsing to work with no joy.</p> <p>This is quite hard for me as I am new to coding. After trying to drop different JS files, references and script into the project I used the Configurator script.</p> <p>I have tried running the code in Explorer and in Firefox; however, the table of results will just not display.</p> <p>The code runs because the page shows the error message: "Error Getting Inventory Data" which is the try/ catch error message in the page code.</p> <p>Could the error message in the text editor error console have something to do with the data not showing?</p> <p>The text editor error console shows the error (Line 76, Column 12) - Unknown attribute: cell spacing in the error console</p> <hr> <p>The Mozilla Firefox error console also shows an error:</p> <pre><code> Error: ReferenceError: YUI is not defined Source File: http://localhost:53753/currency.html Line: 87 </code></pre> <p>The line of code on line 87 that Firefox identifies is:</p> <pre><code>&lt;tr&gt;&lt;td colspan="4"&gt;Click &lt;em&gt;Get Data&lt;/em&gt;&lt;/td&gt;&lt;/tr&gt; </code></pre> <hr> <p>The example code in the YUI library is at: <a href="http://developer.yahoo.com/yui/examples/json/json_convert_values.html" rel="nofollow">http://developer.yahoo.com/yui/examples/json/json_convert_values.html</a></p> <p>Here is my complete code:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;Example: Adding New Object Members During Parsing&lt;/title&gt; &lt;link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"&gt; &lt;link rel="stylesheet" href="css/grids-min.css"&gt; &lt;link rel="stylesheet" href="css/main.css"&gt; &lt;link rel="stylesheet" href="css/prettify-min.css"&gt; &lt;link rel="shortcut icon" type="image/png" href="images/favicon.png"&gt; &lt;/head&gt; &lt;body&gt; &lt;!-- &lt;a href="https://github.com/yui/yui3"&gt;&lt;img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"&gt;&lt;/a&gt; --&gt; &lt;div id="doc"&gt; &lt;div id="hd"&gt; &lt;h1&gt;&lt;img src="http://yuilibrary.com/img/yui-logo.png"&gt;&lt;/h1&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="demo"&gt; &lt;p&gt;Choose a currency, then get the data&lt;/p&gt; &lt;select&gt; &lt;option value="ARS"&gt;Argentine Peso&lt;/option&gt; &lt;option value="AUD"&gt;Australian Dollar&lt;/option&gt; &lt;option value="BRL"&gt;Brazilian Real&lt;/option&gt; &lt;option value="GBP"&gt;British Pound&lt;/option&gt; &lt;option value="CAD"&gt;Canadian Dollar&lt;/option&gt; &lt;option value="CNY"&gt;Chinese Yuan&lt;/option&gt; &lt;option value="COP"&gt;Colombian Peso&lt;/option&gt; &lt;option value="HRK"&gt;Croatian Kuna&lt;/option&gt; &lt;option value="CZK"&gt;Czech Koruna&lt;/option&gt; &lt;option value="DKK"&gt;Danish Krone&lt;/option&gt; &lt;option value="EEK"&gt;Estonian Kroon&lt;/option&gt; &lt;option value="EUR"&gt;Euro&lt;/option&gt; &lt;option value="HKD"&gt;Hong Kong Dollar&lt;/option&gt; &lt;option value="HUF"&gt;Hungarian Forint&lt;/option&gt; &lt;option value="ISK"&gt;Iceland Krona&lt;/option&gt; &lt;option value="INR"&gt;Indian Rupee&lt;/option&gt; &lt;option value="JPY"&gt;Japanese Yen&lt;/option&gt; &lt;option value="KRW"&gt;Korean Won&lt;/option&gt; &lt;option value="LVL"&gt;Latvian Lat&lt;/option&gt; &lt;option value="LTL"&gt;Lithuanian Lita&lt;/option&gt; &lt;option value="MYR"&gt;Malaysian Ringgit&lt;/option&gt; &lt;option value="MXN"&gt;Mexican Peso&lt;/option&gt; &lt;option value="NZD"&gt;New Zealand Dollar&lt;/option&gt; &lt;option value="NOK"&gt;Norwegian Krone&lt;/option&gt; &lt;option value="PHP"&gt;Philippine Peso&lt;/option&gt; &lt;option value="PLN"&gt;Polish Zloty&lt;/option&gt; &lt;option value="RUB"&gt;Russian Rouble&lt;/option&gt; &lt;option value="SGD"&gt;Singapore Dollar&lt;/option&gt; &lt;option value="SKK"&gt;Slovak Koruna&lt;/option&gt; &lt;option value="ZAR"&gt;South African Rand&lt;/option&gt; &lt;option value="LKR"&gt;Sri Lanka Rupee&lt;/option&gt; &lt;option value="SEK"&gt;Swedish Krona&lt;/option&gt; &lt;option value="TRY"&gt;Turkey Lira&lt;/option&gt; &lt;option value="USD" selected="selected"&gt;U.S. Dollar&lt;/option&gt; &lt;option value="CHF"&gt;Swiss Franc&lt;/option&gt; &lt;option value="TWD"&gt;Taiwan Dollar&lt;/option&gt; &lt;option value="THB"&gt;Thai Baht&lt;/option&gt; &lt;/select&gt; &lt;input type="button" id="demo_go" value="Get Data"&gt; &lt;table cellspacing="0"&gt; &lt;caption&gt;Inventory&lt;/caption&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;SKU&lt;/th&gt; &lt;th&gt;Item&lt;/th&gt; &lt;th&gt;Price (USD)&lt;/th&gt; &lt;th&gt;Price (&lt;span&gt;USD&lt;/span&gt;)&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt;&lt;td colspan="4"&gt;Click &lt;em&gt;Get Data&lt;/em&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt; &lt;!-- JS --&gt; &lt;script type="text/javascript" src="http://yui.yahooapis.com/combo?3.8.1/build/yui-base/yui-base-min.js&amp;3.8.1/build/json-parse/json-parse-min.js&amp;3.8.1/build/json-stringify/json-stringify-min.js"&gt;&lt;/script&gt; &lt;script&gt; YUI().use("node", "io", "json-parse",function (Y) { // Safari 4.0.3's native JSON does not support adding members during parse, // so use JavaScript implementation for consistency Y.JSON.useNativeParse = false; var example = { rates : {"USD":1,"EUR":0.6661,"GBP":0.5207,"AUD":1.1225,"BRL":1.609,"NZD":1.4198,"CAD":1.0667,"CHF":1.0792,"CNY":6.8587 ,"DKK":4.9702,"HKD":7.8064,"INR":42.0168,"JPY":109.8901,"KRW":1000,"LKR":107.5269,"MXN":10.1317,"MYR" :3.3167,"NOK":5.3277,"SEK":6.2617,"SGD":1.4073,"THB":33.7838,"TWD":31.1526,"VEF":2.1445,"ZAR":7.6923 ,"BGN":1.3028,"CZK":16.0514,"EEK":10.4275,"HUF":158.7302,"LTL":2.2999,"LVL":0.4692,"PLN":2.1758,"RON" :2.3804,"SKK":20.2429,"ISK":4.8008,"HRK":81.3008,"RUB":24.3309,"TRY":1.1811,"PHP":44.2478,"COP":2000 ,"ARS":3.1289}, currency : 'USD', convert : function (k,v) { // 'this' will refer to the object containing the key:value pair, // so this step will add a new object member while leaving the original // intact (but formatted to two decimal places). If the original // is not needed, just return the converted value. if (k === 'Price') { var x = Math.round(v * example.rates[example.currency] * 100) / 100; this.convertedPrice = x.toFixed(2); // added to item return v.toFixed(2); // assigned to item.Price } return v; }, updateTable : function (inventory) { // Update the column header Y.one('#demo table th span').set('innerHTML',example.currency); var tbody = Y.one('#demo table tbody'), html = ['&lt;table&gt;&lt;tbody&gt;'], rowTemplate = '&lt;tr&gt;&lt;td&gt;{SKU}&lt;/td&gt;&lt;td&gt;{Item}&lt;/td&gt;&lt;td&gt;{Price}&lt;/td&gt;&lt;td&gt;{convertedPrice}&lt;/td&gt;&lt;/tr&gt;', i, len; if (inventory) { for (i = 0, len = inventory.length; i &lt; len; ++i) { html.push(Y.Lang.sub(rowTemplate, inventory[i])); } } else { html.push('&lt;tr&gt;&lt;td colspan="4"&gt;No Inventory data&lt;/td&gt;&lt;/tr&gt;'); } html.push('&lt;/tbody&gt;&lt;/table&gt;'); tbody.replace(Y.Node.create(html.join('')).one('tbody')); } }; Y.one('#demo_go').on('click', function (e) { // Disable the button temporarily this.set('disabled',true); // Store the requested currency var sel = Y.one("#demo select"); example.currency = sel.get("options").item(sel.get("selectedIndex")).get("value"); Y.io('js/json-convert-values-response.json',{ timeout : 3000, on : { success : function (xid, res) { var inventory; try { inventory = Y.JSON.parse(res.responseText,example.convert); example.updateTable(inventory); } catch(e) { alert("Error getting inventory data"); } finally { Y.one('#demo_go').set('disabled',false); } }, failure : function () { alert("Error getting inventory data"); } } }); }); // Expose example objects for inspection YUI.example = example; }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>UPDATE - Apparently these JSON examples will not work on jsfiddle since they are using XHR to fetch the JSON data. That data doesn't exist on their server, so it will file. SO PLEASE IGNORE THE FIDDLE</strong></p> <p>Here is a JS Fiddle of my problem, can anyone please help me to get this working?</p> <p>Adding New Object Members During Parsing: <a href="http://jsfiddle.net/DanyB/3ntvw/" rel="nofollow">http://jsfiddle.net/DanyB/3ntvw/</a></p> <p>Thank you, to anyone kind enough to take a look.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload