سؤال

I am requesting json file every few seconds and it's sometimes quite big, so is it possible to somehow retrieve not the whole json file, but just the last element? getJson gets the whole file.

هل كانت مفيدة؟

المحلول

No, you must fetch the entire file before you can parse it and return only the last element. The best solution here is to create a new JSON file with only the information you need (if you have that ability).

نصائح أخرى

Not unless you either:

  • Modify the service to provide only the last element, possibly at a different URL/using parameters.
  • Know exactly how long the data before the last element will be, allowing you to do a [RANGE][1] request to the server

Of course, with the second method, the last element on its own is likely not a valid JSON object (not guaranteed to be enclosed in curly braces) so you'll need to manipulate it before parsing it

Well you can't access partial json from javascript. You have to do it on the server side. Just create a new php function that reads the last element and access it from getJson

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top