3

I know this isn't the best way to do it, but I have no other choice :(

I have to access the items in JSONObject by their index. The standard way to access objects is to just wirte this[objectName] or this.objectName. I also found a method to get all the fields inside a json object:

(for (var key in p) { 
  if (p.hasOwnProperty(key)) { 
    alert(key + " -> " + p[key]); 
  } 
} 

(Soruce : Loop through Json object).

However there is no way of accessing the JSONfields directly by a index. The only way I see right now, is to create an array, with the function above, get the fieldname by index and then get the value by fieldname.

As far as I see it, the p (in our case the JSON file must be an iteratable array to, or else the foreach loop wouldn't work. How can I access this array directly? Or is it some kind of unsorted list?

Community
  • 1
  • 1
Stefan
  • 14,826
  • 17
  • 80
  • 143
  • Why do you have to access the items by their index? What you've written is how to iterate through an object, which is what you have -- changing it to an array doesn't provide the same information. – Waynn Lue Mar 19 '12 at 10:01
  • hi, I can't get the the data any other way, so I somehow access them via index (the idea is to display in a table with all fields as one column) since we want to use the same format as for other objects (where we access them by fieldnames), I have to find a way to speciffic access the data by index :(. So as mentioned, I create an array of the fieldnames first and then access them via index, but I want to get rid of this inderection :P – Stefan Mar 19 '12 at 10:07
  • Why not just have a variable index before you iterate, then use that to have an order? Like `var i` before the for loop, and i = i + 1 inside it? – Waynn Lue Mar 19 '12 at 10:09

4 Answers4

9

A JSON Object is more like a key-value-map; so, yes, it is unsorted. The only way to get around is the index->property name map you've already mentioned:

var keysbyindex = Object.keys(object);
for (var i=0; i<keysbyindex.length; i++)
    alert(object[keysbyindex[i]]);

But why would you need these indexes? A unsorted map also has no length property, as an Array had. Why don't you use the for-in-loop

var counter = 0; // if you need it
for (var key in object) {
    alert(object[key])
    counter++;
}

? If you have a parsed JSON object, i.e. a plain JS Object, you won't have to worry about enumerable prototype properties.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
2

Based on Bergis anserwer this is my solution:

var keysbyindex = Object.keys(this);
alert(this[keysbyindex[index]]);        
return this[keysbyindex[index] || ""];

However, I think (not tested) it's extremly bad regaring performace and shouldn't be used! But desperate times require desperate measures.....

Stefan
  • 14,826
  • 17
  • 80
  • 143
1

I don't think you can actually achieve this without creating your own parsing of JSON. You're writing that you want to go trough a JSON-object, but what you're actually trying to do is go trough a plain old Javascript object. Json is simply a string-representation used to transfer/store said object, and in here lies the main problem: the parser that transforms the string into an actual object (ie. the browser in most cases) can chose to ignore the order it finds the properties if it want to. Also, different browsers might have different approaches to parsing JSON for all you know. If they simply use a hash-map for the object that it's simple to loop through it, but the order won't be dependent on the order of the keys in the file, but rather the keys themselves.

For example, if you have the json {"b":"b","a":"a"} and do the for in loop, under some implementations you might end up with a comming first, and in others you might end up with b.

Alxandr
  • 12,345
  • 10
  • 59
  • 95
  • Yes, that's true. Yet I've never seen an implementation that does the for-in-loop not in the notation order, do you know differing ones? – Bergi Mar 19 '12 at 10:57
0

var jsn = {keyName: 'key value result come here...'};
var arr = jsn ? $.map(jsn, function (el) { return el }) : [0];

console.log(arr[0])
$('.result').text(arr[0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="result"></span>
M. Salah
  • 671
  • 7
  • 10