简体   繁体   English

从Google Apps脚本中的GET API调用解析JSON数组

[英]Parse a JSON Array from a GET API Call in google apps script

I use a GET API call to an endpoint and get the output as follows: 我使用对端点的GET API调用并获得如下输出:

  [
    {
        "available": false,
        "occasional": false,
        "id": 36005656995,
        "signature": "<div dir=\"ltr\"><p><br></p>\n</div>",
        "ticket_scope": 1,
        "created_at": "2018-06-20T10:13:25Z",
        "updated_at": "2018-08-29T06:37:21Z",
        "available_since": null,
        "contact": {
            "active": false,
            "email": "abcd@clasher.com",
            "job_title": null,
            "language": "en",
            "last_login_at": "2018-08-15T05:50:27Z",
            "mobile": null,
            "name": "abcd@clasher.com",
            "phone": "00903030333",
            "time_zone": "Chennai",
            "created_at": "2018-03-19T01:56:53Z",
            "updated_at": "2018-09-03T04:41:59Z"
        }
    },
    {
        "available": false,
        "occasional": true,
        "id": 36004999676,
        "signature": "<div dir=\"ltr\"><p><br></p>\n</div>",
        "ticket_scope": 1,
        "created_at": "2018-03-07T05:47:43Z",
        "updated_at": "2018-06-21T12:45:41Z",
        "available_since": null,
        "contact": {
            "active": true,
            "email": "sab@clashuniversal.com",
            "job_title": null,
            "language": "en",
            "last_login_at": "2018-03-07T05:49:16Z",
            "mobile": null,
            "name": "Sample Agent",
            "phone": null,
            "time_zone": "Chennai",
            "created_at": "2018-03-07T05:47:43Z",
            "updated_at": "2018-05-28T10:39:39Z"
        }
    },
    {
        "available": false,
        "occasional": false,
        "id": 36004979341,
        "signature": "<div dir=\"ltr\"><p>Regards<br>Clashuniversal</p></div>",
        "ticket_scope": 1,
        "created_at": "2018-03-06T15:27:59Z",
        "updated_at": "2018-10-02T19:51:12Z",
        "available_since": "2018-08-10T14:10:09Z",
        "contact": {
            "active": true,
            "email": "prasadclasher@gmail.com",
            "job_title": null,
            "language": "en",
            "last_login_at": "2018-09-30T06:02:21Z",
            "mobile": null,
            "name": "Subramania Prasad",
            "phone": "9999999998",
            "time_zone": "Chennai",
            "created_at": "2018-03-06T15:27:59Z",
            "updated_at": "2018-09-20T08:43:18Z"
        }
    }
]

Note that the response as such is a JSON Array. 请注意,响应本身就是JSON数组。

What I want to do is to parse this into a Javascript object so that I can do a bit of processing on this data. 我想要做的是将其解析为Javascript对象,以便可以对此数据进行一些处理。

For parsing this data I am using the following code: 为了解析此数据,我使用以下代码:

test_response = UrlFetchApp.fetch(domain,obj);
test_resp_string = test_response.getContentText();
object_1 = JSON.parse(test_resp_string);

The expectation of above code is that I would like to access each of the object of the response separately for example when I do Logger.log(object_1[0]) should give me the first element of the returned response as below: 上面的代码期望我想分别访问响应的每个对象,例如,当我执行Logger.log(object_1 [0])应该给我返回的响应的第一个元素时,如下所示:

{
        "available": false,
        "occasional": false,
        "id": 36005656995,
        "signature": "<div dir=\"ltr\"><p><br></p>\n</div>",
        "ticket_scope": 1,
        "created_at": "2018-06-20T10:13:25Z",
        "updated_at": "2018-08-29T06:37:21Z",
        "available_since": null,
        "contact": {
            "active": false,
            "email": "abcd@clasher.com",
            "job_title": null,
            "language": "en",
            "last_login_at": "2018-08-15T05:50:27Z",
            "mobile": null,
            "name": "abcd@clasher.com",
            "phone": "00903030333",
            "time_zone": "Chennai",
            "created_at": "2018-03-19T01:56:53Z",
            "updated_at": "2018-09-03T04:41:59Z"
        }
    }

However when I do Logger.log(object_1[0]), I get the following response: 但是,当我执行Logger.log(object_1 [0])时,得到以下响应:

 {
  updated_at=2018-08-29T06:37:21Z, 
  signature=<div dir="ltr"><p><br></p></div>, 
  ticket_scope=1, 
  contact={
  last_login_at=2018-08-15T05:50:27Z, 
  updated_at=2018-09-03T04:41:59Z, 
  phone=00903030333, 
  mobile=null, 
  name=abcd@clasher.com, 
  active=false, 
  created_at=2018-03-19T01:56:53Z, 
  language=en, 
  time_zone=Chennai, 
  job_title=null, 
  email=abcd@clasher.com}, 
  available=false, 
  created_at=2018-06-20T10:13:25Z, 
  occasional=false, 
  id=3.6005656995E10, 
  available_since=null
}

You'd be able to notice the following differences between what is expected and what I get: 您将能够注意到预期和我得到的之间的以下差异:

  1. In the response that I get the order of the output is varied, I'm curious to know why it is so,For-ex. 在我得到输出顺序变化的响应中,我很好奇知道为什么如此。 In the response of original call the first key is "available" while after parsing the data it is "updated_at" 在原始呼叫的响应中,第一个键为“ available”,而在解析数据后为“ updated_at”

  2. The id key has a value say 36005656995 in the original response, however after converting it into a javascript object using JSON.parse the id value is 3.6005656995E10 . id关键字在原始响应中的值为36005656995 ,但是在使用JSON.parse将其转换为javascript对象后,id值为3.6005656995E10 Because of this conversion, I'm not able to do the post processing. 由于这种转换,我无法进行后期处理。 I also could not find a way to explicitly convert it into a string(so that the number is preserved exactly as such in the original response)while parsing JSON . 我也找不到在解析JSON时将其显式转换为字符串的方法(这样,数字将保留为原始响应中的形式)

I was able to use to.String() to convert the number to the original ID seen in the response, however I'm still curious to find out the answer for the following query: 我能够使用to.String()将数字转换为响应中看到的原始ID,但是我仍然很想知道以下查询的答案:

How do I preserve the value of ID from the original response without converting it into float or long 如何保留原始响应中的ID值而不将其转换为float或long

I hope I was clear with my query, thanks in advance for going through the long query. 希望我对查询清楚,在此先感谢您通过冗长的查询。

When you use JSON.parse() , the payload string is converted to an array of objects. 使用JSON.parse() ,有效负载字符串将转换为对象数组。 The order of the items in the top-level array will be retained. 顶层数组中项目的顺序将保留。 However each array item is an object and the order of the properties on those objects is not guaranteed, nor does it need to be since you'll be referencing those properties by name (using either dot notation or bracketed lookup). 但是,每个数组项都是一个对象,并且不能保证这些对象上的属性的顺序,也不一定是这样,因为您将通过名称(使用点符号或带括号的查找)引用这些属性。

The id key is fine, its just being represented in scientific notation which is standard for large numbers. id键很好,它只是用科学记数法表示,这是数字的标准。 If you need the number in pure decimal format as a string use the toFixed() method to convert it. 如果需要纯十进制格式的数字作为字符串,请使用toFixed()方法进行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM