如何在json回调中删除“结果”:“成功”

时间:2014-03-27 10:53:07

标签: php json

调用api服务器的结果是一个json文件,以这个字符串开头:

{
    "result": "success"
    ,   "data": {"total":16080,"pageCount":161,"result":[{"packWidth":250,"itemNo"

如何删除我不在乎的部分? 就是这个

{
        "result": "success"
        ,   "data": {"total":16080,"pageCount":161,"result":

完整的结果是:

{
    "result": "success"
    ,   "data": {"total":16080,"pageCount":161,"result":    [{"packWidth":250,"itemNo":"1203945","groupItemNo":"1203945","status":1,"categoryId":105096,"packType":"Color Box","barcode":"6922833439687","modelLabel":"Color","packQty":24,"packInclude":"USB Cable, User Manual, USB Charger, Earphone, 1pcs Li-Battery, LCD Protector, Leather Case, Plastic Case","clearance":false,"id":103928,"packWeight":"12.500","price":"181.2800","packLength":400,"description":"description test","unitWeight":"0.726","packHeight":300}]}}

我使用PHP语言

我必须删除初始部分:

{
    "result": "success"
    ,   "data": {"total":16080,"pageCount":161,"result":

和决赛:

}}

2 个答案:

答案 0 :(得分:3)

如果要使用JSON的一部分来填充CSV文件,请使用json_decode方法解析json并访问必要的信息。

尝试这样的事情:

var jsonObject = json_decode(myJson);
var interestingPart = jsonObject.data.result;

您现在可以以对象方式访问数据。或者如果你想从中获取一个json,那么使用:

var interestingJson = json_encode(interestingPart);

答案 1 :(得分:0)

未经测试,但应该可以使用

preg_replace("/^.*?:(?=\w*\[) | (\w*}\w*}\w*$)/", "", $str); 

编辑:我在知道json_decode之前写过这个,你应该真的使用像fazovskys回答中建议的json函数。