H Min H Min - 3 months ago 15
JSON Question

get value of last occurrence of json element in javascript

http://hubtest.atspace.cc/json.html

in the above JSON response, I need to obtain the LAST objectId value to use as part of another URL. What is the best way to do this with javascript?

I attempted it with regex, but it got too messy. I'm assuming there is an easier way.

Answer

Try JSON.parse(...) on the source (since it is literally JSON) and then just treat it like an object literal.

Since it's an array you can do var array = JSON.parse(...) then get array[array.length - 1].objectID.

Comments