使用php ajax计算json数据

时间:2017-06-27 12:34:10

标签: javascript php json ajax

只需要获取json数据的特定计数

这里是样本json格式:

{
  "tickets": [
    {
      "url": "https://asd.zendesk.com/api/v2/tickets/1.json",
      "id": 1,
      "external_id": null,
      "via": {
        "channel": "sample_ticket",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2017-04-25T05:56:36Z",
      "updated_at": "2017-04-25T05:56:36Z",
      "status": "open",
    },
    {
      "url": "https://asd.zendesk.com/api/v2/tickets/2.json",
      "id": 2,
      "external_id": null,
      "via": {
        "channel": "sample_ticket",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2017-04-25T05:56:36Z",
      "updated_at": "2017-04-25T05:56:36Z",
      "status": "close",
    },
    {
      "url": "https://asd.zendesk.com/api/v2/tickets/2.json",
      "id": 2,
      "external_id": null,
      "via": {
        "channel": "sample_ticket",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2017-04-25T05:56:36Z",
      "updated_at": "2017-04-25T05:56:36Z",
      "status": "solve",
    }
    ]
}

我想计算使用php或ajax或两者打开,关闭和解决状态的数量...还需要数据自动加载......所以我们不需要刷新..

1 个答案:

答案 0 :(得分:2)

检查出来:

var jsonObject = {
  "tickets": [
    {
      "url": "https://asd.zendesk.com/api/v2/tickets/1.json",
      "id": 1,
      "external_id": null,
      "via": {
        "channel": "sample_ticket",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2017-04-25T05:56:36Z",
      "updated_at": "2017-04-25T05:56:36Z",
      "status": "open",
    },
    {
      "url": "https://asd.zendesk.com/api/v2/tickets/1.json",
      "id": 1,
      "external_id": null,
      "via": {
        "channel": "sample_ticket",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2017-04-25T05:56:36Z",
      "updated_at": "2017-04-25T05:56:36Z",
      "status": "open",
    },
    {
      "url": "https://asd.zendesk.com/api/v2/tickets/2.json",
      "id": 2,
      "external_id": null,
      "via": {
        "channel": "sample_ticket",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2017-04-25T05:56:36Z",
      "updated_at": "2017-04-25T05:56:36Z",
      "status": "close",
    },
    {
      "url": "https://asd.zendesk.com/api/v2/tickets/2.json",
      "id": 2,
      "external_id": null,
      "via": {
        "channel": "sample_ticket",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2017-04-25T05:56:36Z",
      "updated_at": "2017-04-25T05:56:36Z",
      "status": "solve",
    }
    ]
};

var open = 0, close = 0; solve = 0;
for( var i = 0; i < jsonObject.tickets.length; i++)
{
  if(jsonObject.tickets[i].status == "open")
  {
   open++;
  }
  else if(jsonObject.tickets[i].status == "close")
  {
   close++;
  }
  else if(jsonObject.tickets[i].status == "solve")
  {
   solve++;
  }

}

console.log(open, close, solve);