Json Decode Joomla Params

时间:2014-12-09 15:31:25

标签: json joomla params

PARAMS:

  

{"calculations":"0","listlayout":"author_items","resetfilters":"1","list_elements":"null","prefilters":"{\"filter-join\":[\"AND\"],\"filter-fields\":[\"uh46v_fb_sw_author_items.category_lev2_raw\"],\"filter-conditions\":[\"equals\"],\"filter-value\":[\"9\"],\"filter-eval\":[\"0\"],\"filter-access\":[\"1\"]}","rows_per_page":"10","menu-anchor_title":"","menu-anchor_css":"","menu_image":"","menu_text":1,"page_title":"Food & Beverages","show_page_heading":1,"page_heading":"Food & Beverages","pageclass_sfx":"","menu-meta_description":"","menu-meta_keywords":"","robots":"","secure":0}

如何获得过滤值9? \"filter-value\":[\"9\"]

我已尝试使用Json_decode,但它无法正常工作。

4 个答案:

答案 0 :(得分:2)

你需要一些像这样的json-object-array操作:

$testString = '{"calculations":"0","listlayout":"author_items","resetfilters":"1","list_elements":"null","prefilters":"{\"filter-join\":[\"AND\"],\"filter-fields\":[\"uh46v_fb_sw_author_items.category_lev2_raw\"],\"filter-conditions\":[\"equals\"],\"filter-value\":[\"9\"],\"filter-eval\":[\"0\"],\"filter-access\":[\"1\"]}","rows_per_page":"10","menu-anchor_title":"","menu-anchor_css":"","menu_image":"","menu_text":1,"page_title":"Food & Beverages","show_page_heading":1,"page_heading":"Food & Beverages","pageclass_sfx":"","menu-meta_description":"","menu-meta_keywords":"","robots":"","secure":0}';

$testString = json_decode(json_decode($testString)->prefilters)->{'filter-value'}[0];

$ testString将变为9。

答案 1 :(得分:0)

好的,你可以这样做并检查它是如何工作的:

$filter = json_decode('{"calculations":"0","listlayout":"author_items","resetfilters":"1","list_elements":"null","prefilters":"{\"filter-join\":[\"AND\"],\"filter-fields\":[\"uh46v_fb_sw_author_items.category_lev2_raw\"],\"filter-conditions\":[\"equals\"],\"filter-value\":[\"9\"],\"filter-eval\":[\"0\"],\"filter-access\":[\"1\"]}","rows_per_page":"10","menu-anchor_title":"","menu-anchor_css":"","menu_image":"","menu_text":1,"page_title":"Food & Beverages","show_page_heading":1,"page_heading":"Food & Beverages","pageclass_sfx":"","menu-meta_description":"","menu-meta_keywords":"","robots":"","secure":0}');

echo '<pre>' ; 
print_r($filter);

$prefilter = json_decode($filter->prefilters);
print_r($prefilter);

现在访问它:

$prefilter->{'filter-value'}[0] // gives 9

答案 2 :(得分:0)

取决于您使用的内容(在我的情况下,我使用基于YooTheme Warp Framework的主题,我在自定义样式中自定义窗口小部件的输出,以及窗口小部件/模块的参数公开为Joomla\Registry\Registry下的$widget->parameter对象以及$widget->params下的JSON字符串,您可以通过调用适当的$obj->get('my_key')方法。

答案 3 :(得分:0)

实际上,Joomla!有一个处理params的本地方式......所以永远不要 - json解码params字符串手动......

而是这样做

$paramsString = '{"calculations":"0","listlayout":"author_items","resetfilters":"1","list_elements":"null","prefilters":"{\"filter-join\":[\"AND\"],\"filter-fields\":[\"uh46v_fb_sw_author_items.category_lev2_raw\"],\"filter-conditions\":[\"equals\"],\"filter-value\":[\"9\"],\"filter-eval\":[\"0\"],\"filter-access\":[\"1\"]}","rows_per_page":"10","menu-anchor_title":"","menu-anchor_css":"","menu_image":"","menu_text":1,"page_title":"Food & Beverages","show_page_heading":1,"page_heading":"Food & Beverages","pageclass_sfx":"","menu-meta_description":"","menu-meta_keywords":"","robots":"","secure":0}'

$params = class_exists('JParameter') ? new JParameter($paramsString) : new JRegistry($paramsString);

然后检索这样的参数

$params->get('menu_image', false);

get方法的第一个参数是您尝试获取的参数,第二个参数是默认值 - 如果param不存在或为空