【问题标题】:json to javascript variable zendeskjson 到 javascript 变量 zendesk
【发布时间】:2018-07-04 08:58:34
【问题描述】:

我正在尝试从 zendesk api 中为特定用户检索打开的票证数量。但是我似乎无法让它工作。我不断收到此错误:

Uncaught TypeError: Cannot read property '500' of undefined

json格式:

{
"user": {
    "id": 500,
    "url": "https://zendesk/api/v2/users/500.json",
    "name": "Random name",
    "email": "not important",
    "created_at": "2016-05-18T15:26:43Z",
    "updated_at": "2018-07-04T06:23:35Z",
    "time_zone": "Brussels",
    "phone": null,
    "shared_phone_number": null,
    "photo": {
        "url": "https://google.com",
        "id": 504,
        "file_name": "keep-calm-and-shut-up-im-your-system-administrator.png",
        "content_url": "https://google.com",
        "mapped_content_url": "https://google.com",
        "content_type": "image/png",
        "size": 3298,
        "width": 80,
        "height": 50,
        "inline": false,
        "thumbnails": [
            {
                "url": "https://google.com",
                "id": 90752965,
                "file_name": "not important",
                "content_url": "https://google.com",
                "mapped_content_url": "https://google.com",
                "content_type": "image/png",
                "size": 3298,
                "width": 32,
                "height": 20,
                "inline": false
            }
        ]
    },
    "locale_id": 1005,
    "locale": "nl",
    "organization_id": 501,
    "role": "admin",
    "verified": true,
    "external_id": null,
    "tags": [],
    "alias": "",
    "active": true,
    "shared": false,
    "shared_agent": false,
    "last_login_at": "2018-07-04T06:23:35Z",
    "two_factor_auth_enabled": null,
    "signature": "",
    "details": "",
    "notes": "",
    "role_type": null,
    "custom_role_id": null,
    "moderator": true,
    "ticket_restriction": null,
    "only_private_comments": false,
    "restricted_agent": false,
    "suspended": false,
    "chat_only": false,
    "default_group_id": 503,
    "user_fields": {
        "agent_ooo": false
    }
},
"open_ticket_count": {
    "500": 15
}}

这是我的 javascript 代码:

        <script>
    function getJSON(url) {
    var resp ;
    var xmlHttp ;

    resp  = '' ;
    xmlHttp = new XMLHttpRequest();

    if(xmlHttp != null)
    {
        xmlHttp.open( "GET", url, false );
        xmlHttp.send( null );
        resp = xmlHttp.responseText;
    }

    return resp ;
}
   var gjson ;
    gjson = getJSON('https://zendesk.com//api/v2/users/me.json? 
    include=open_ticket_count');

console.log(gjson.open_ticket_count["500"]);

</script>

有人可以帮帮我吗?我不确定我做错了什么(zendesk url 是实际脚本中的正确 url,他们可以访问它)

TLDR:我需要从 json 中检索变量:open_ticket_count。

谢谢!

【问题讨论】:

    标签: javascript html json zendesk


    【解决方案1】:

    您的getJSON 函数不会等待请求实际通过。像这样的函数只有在完成后才会返回 responseText:

    const getJSON = function(url, callback) {
        let xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.responseType = 'json';
        xhr.onload = function() {
          let status = xhr.status;
          if (status === 200) {
            callback(null, xhr.response);
          } else {
            callback(status, xhr.response);
          }
        };
        xhr.send();
    };
    

    然后您可以使用它来获取 Zendesk JSON:

    getJSON('https://zendesk.com//api/v2/users/me.json?include=open_ticket_count', (status, gjson) => {
        console.log(gjson.open_ticket_count["500"]);
    });
    

    【讨论】:

      【解决方案2】:

      如果没有其他环境,很难说准确,但我认为这会起作用:

          var gjson ;
          gjson = getJSON('https://zendesk.com//api/v2/users/me.json?include=open_ticket_count'');
          var jsonObj = JSON.parse(gjson); // assuming getJSON returns the json as string, this is async, make sure next row has the data needed on time or rewqork this as promise
          console.log(jsonObj.open_ticket_count["500"]);
      

      所以基本上调用整个 JSON,然后将其从字符串解析为对象,然后像对象一样使用它

      【讨论】:

        【解决方案3】:

        线索在错误中

        未捕获的类型错误:无法读取未定义的属性“500”

        这说明gjson.open_ticket_count 是未定义的。

        您实际上还没有解析 JSON 并试图获取字符串的属性而不是解析的 JSON。

        先尝试解析。

        var gjson;
        gjson = getJSON('https://zendesk.com//api/v2/users/me.json?include=open_ticket_count');
        var gobj = JSON.parse(gjson);
        console.log(gobj.open_ticket_count["500"]);
        

        【讨论】:

          【解决方案4】:

          您需要解析 JSON 才能访问它。使用下面的代码

          <script>
              function getJSON(url) {
                 var resp ;
                 var xmlHttp ;
          
                 resp  = '' ;
                 xmlHttp = new XMLHttpRequest();
          
                 if(xmlHttp != null)
                 {
                     xmlHttp.open( "GET", url, false );
                     xmlHttp.send( null );
                     resp = xmlHttp.responseText;
                 }
          
                 return resp ;
              }
          
              var gjson ;
              gjson = getJSON('https://zendesk.com//api/v2/users/me.json? 
              include=open_ticket_count');
          
              gjson = JSON.parse(gjson)
          
              console.log(gjson.open_ticket_count["500"]);
          
          </script>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-12-06
            • 2014-09-11
            • 2018-03-03
            • 2011-02-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多