Given this object:
:
gd:
openSearch: .1/
app: ;
media: /
How do I get the value of the first property? I suspect this is an easy process, but I'm drawing a blank. Thanks in advance.
The object is built as such:
Server side (php):
$namespaces = $feedXML->getNamespaces(true);
$arr = array(
'Status' => 'Success',
'Message' => 'Feed fetched.',
'Namespaces' => $namespaces,
'Feed XML' => $feedXML
);
echo json_encode($arr);
Client side (JS):
var output = '';
for (property in dataj["Namespaces"]) {
output += property + ': ' + dataj["Namespaces"][property] + '; ';
}
alert(output);
I would like to be able to check the namespaces to see if this is Atom or RDF.
It sounds like just iterating each property is going to be the best way.
Given this object:
: http://www.w3/2005/Atom
gd: http://schemas.google./g/2005
openSearch: http://a9./-/spec/opensearch/1.1/
app: http://www.w3/2007/app;
media: http://search.yahoo./mrss/
How do I get the value of the first property? I suspect this is an easy process, but I'm drawing a blank. Thanks in advance.
The object is built as such:
Server side (php):
$namespaces = $feedXML->getNamespaces(true);
$arr = array(
'Status' => 'Success',
'Message' => 'Feed fetched.',
'Namespaces' => $namespaces,
'Feed XML' => $feedXML
);
echo json_encode($arr);
Client side (JS):
var output = '';
for (property in dataj["Namespaces"]) {
output += property + ': ' + dataj["Namespaces"][property] + '; ';
}
alert(output);
I would like to be able to check the namespaces to see if this is Atom or RDF.
It sounds like just iterating each property is going to be the best way.
Share Improve this question edited Dec 12, 2012 at 21:45 Drazisil asked Dec 12, 2012 at 21:17 DrazisilDrazisil 3,3934 gold badges35 silver badges57 bronze badges 5-
Does that code actually work? Because all I get is Uncaught SyntaxError: Unexpected token
:
. – David Thomas Commented Dec 12, 2012 at 21:20 - What's the exact code ? How is built your object ? – Denys Séguret Commented Dec 12, 2012 at 21:21
-
This is not valid JavaScript object. And I am sure the api sends the properly encoded JSON object (e.g.
{"xmlns": "http://www.w3/2005/Atom", "xmlns$gd": "http://schemas.google./g/2005"}
). – Salman Arshad Commented Dec 12, 2012 at 21:21 - @SalmanA I have no idea what api you are talking about – Drazisil Commented Dec 12, 2012 at 21:27
- Can you post (relevant part of) the JSON generated by your PHP script? – Salman Arshad Commented Dec 12, 2012 at 21:29
3 Answers
Reset to default 10If you're trying to get the value of the property whose key is an empty string, then you can do
var value = myObject[''];
If you try to get the "first property" of an object, you can't because properties in javascript objects aren't ordered.
Properties aren't guaranteed to be ordered. You can however iterate over all properties to find the right one (if you know what you are looking for):
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
doSomethingWith(obj[prop]);
}
Reference: Iterating over every property of an object in javascript using Prototype?
Then get the key by
var value = obj[key];
You can try this code:
var test_bject = {'test': 1, 'test2': 2, 'test3': 3}, first_value;
for (i in test) {
first_value = test_object[i];
break;
}