You are here

function views_json_get in Views Datasource 6

Same name and namespace in other branches
  1. 7 views_json.module \views_json_get()

Gets JSON data from a View rendered in the JSON data document style.

This is useful for when working with a JSON view in code.

Parameters

$name: The name of the view.

$display_id: The display of the view to use.

$args: The arguments to pass to the view.

$raw: If TRUE, the JSON data is returned as a string. Otherwise, an object representation is returned.

Return value

The JSON data in the form of an object or a string or NULL otherwise.

File

./views_json.module, line 207
Module definition for views_json

Code

function views_json_get($name, $display_id = 'default', $args = array(), $raw = FALSE) {
  $view = views_get_view($name);
  if (!is_object($view)) {
    return NULL;
  }
  $preview = $view
    ->preview($display_id, $args);
  $start_pos = strpos($preview, '{');
  $finish_pos = strrpos($preview, '}');
  $length = $finish_pos - $start_pos + 1;
  $json = trim(substr($preview, $start_pos, $length));
  if ($raw) {
    return $json;
  }
  return json_decode($json);
}