如何从webservices中调用数据,如何获取(subcategoriescount)值

时间:2015-06-16 15:39:37

标签: javascript

这是我的代码:

{"subcategories":[],"subcategoriescount":"0","catname":"Gloves","weight":"","products":[{ 
                            "product_id":"2584",}]

1 个答案:

答案 0 :(得分:1)

你的JSON数据不起作用,所以我添加了丢失的最后一个花括号并在“2584”之后删除了逗号,然后它工作了(PHP对JSON语法非常敏感)。一旦从Web服务获得JSON数据,就会以这种方式获得“subcategoriescount”:

<?php

$my_data = '{"subcategories":[],"subcategoriescount":"0","catname":"Gloves","weight":"","products":[{"product_id":"2584"}]}';

$my_object = json_decode( $my_data ); // CONVERT FROM JSON TO OBJECT.

echo "Subcategoriescount value is = " .
     $my_object->{"subcategoriescount"};  // WILL DISPLAY "0".

?>

要测试以前的代码,请创建一个文本文件,为其指定任何带有PHP扩展名的名称,复制粘贴代码,打开浏览器并运行localhost/anyname.php。为了确保它正常工作,转到JSON字符串并替换“subcategoriescount”的值,从“0”到“5”(或任何其他),您应该在屏幕上看到类似的内容:

Subcategoriescount value is = 5