2

I understand concepts of JSON ok, but after starting to use ebay's api, I came across a notation which I've not seen before, and was wondering if anyone could explain what's going on with it?

{ "findItemsByKeywordsResponse": [ { "ack": [ "Success" ], "version": [ "1.5.0" ], "timestamp": [ "2010-06-16T08:42:21.468Z" ], "searchResult": [ { "@count": "0" } ], "paginationOutput": [ { "pageNumber": [ "0" ], "entriesPerPage": [ "10" ], "totalPages": [ "0" ], "totalEntries": [ "0" ] } ] } ] 

}

What's the "@count" thing? I noticed when I reference it in chrome, it throws an error:

chrome error http://www.oth4.com/clip.jpg

But in Firefox not. JSON Lint reports it's valid, as I'd expect... ;)

1
  • 1
    Is this JSON created from XML? Because E4X represents attributes using @attribute_name syntax. Commented Jun 16, 2010 at 9:06

4 Answers 4

6

It is a property name that starts with an @ character. That is all.

Use square bracket notation to access properties containing characters that you can't use in dot notation.

i.e.

currentPrice[0]['@currencyId'] 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this worked perfectly. There's nothing special at all about this "@" notation then?
Nothing at all, at least not as far as JSON is concerned.
3

In addition to the answers here, @ usually appears in JSON property names when the JSON is created from XML. The @ represents an XML attribute so that it can be distinguished from the child elements of that XML node in it's new JSON form. For instance, that particular item in XML might look like this:

 <searchResult count="0"> </searchResult> 

As already suggested, you can access the property using square bracket notation.

Comments

2

Try:

var currency = item.sellingStatus[0].currentPrice[0]["@currencyId"]; 

There's no requirement that Javascript array keys be valid Javascript identifiers.

1 Comment

Thanks also, worked too, but will accept above for useful link.
0

In PHP, if you want to access a property which name starts with an invalid character you have to use {'property_name'}. So if you want to access @count from your specific JSON example you should try this:

$json_decoded = json_decode($json_var); $count = $json_decoded->findItemsByKeywordsResponse[0]->searchResult[0]->{'@count'}; 

json_decode() function was used to convert the JSON into an PHP object.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.