Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript Object Iteration
    primarykey
    data
    text
    <p>I am having trouble accessing certain properties in an object that I receive as JSON from a server-side script, as the result of an AJAX call that I am making. My JSON data can come in one of two forms, depending on the data in the object itself.</p> <p>Form 1:</p> <pre><code> "MY_OBJ" : { "FILE" : "myfile.txt", "PARAMS" : { "PARAM" : { "KEY" : "mykey", "VALUE" : "myvalue" } } } </code></pre> <p>Form 2:</p> <pre><code> "MY_OBJ" : { "FILE" : "myfile.txt", "PARAMS" : { "PARAM" : [ { "KEY" : "mykeyone", "VALUE" : "myvalueone" }, { "KEY" : "mykeytwo", "VALUE" : "myvaluetwo" } ] } } </code></pre> <p>This is how I am currently trying to parse the data to display in the browser:</p> <pre><code> function(v) { var myFormattedData = v.FILE; if (v.PARAMS !== undefined &amp;&amp; v.PARAMS.PARAM !== undefined &amp;&amp; v.PARAMS.PARAM.KEY !== undefined &amp;&amp; v.PARAMS.PARAM.VALUE !== undefined) { myFormattedData += '&lt;br /&gt;' + v.PARAMS.PARAM.KEY + ' : ' + v.PARAMS.PARAM.VALUE; } } </code></pre> <p>This method works fine when my data is in Form 1. In that situation I will get output in the browser like...</p> <pre><code>myfile.txt mykey : myvalue </code></pre> <p>...which is exactly what I want.</p> <p>However, when the data is in Form 2 all I get is the file name displayed in the browser like this...</p> <pre><code>myfile.txt </code></pre> <p>... but what I am trying to get is something like...</p> <pre><code>myfile.txt mykeyone : myvalueone mykeytwo : myvaluetwo </code></pre> <p>I need to be able to handle getting the data in both forms. What's worse is that I probably also should plan for the possibility of receiving more than just one or two key-value pairs at some point in the future. I have been struggling with this for a long time. Any help is much appreciated!</p> <p>Thank you!</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.
    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