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@*er.com",
"job_title": null,
"language": "en",
"last_login_at": "2018-08-15T05:50:27Z",
"mobile": null,
"name": "abcd@*er.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@*universal.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>*universal</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": "prasad*er@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@*er.com",
"job_title": null,
"language": "en",
"last_login_at": "2018-08-15T05:50:27Z",
"mobile": null,
"name": "abcd@*er.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@*er.com,
active=false,
created_at=2018-03-19T01:56:53Z,
language=en,
time_zone=Chennai,
job_title=null,
email=abcd@*er.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:
您将能够注意到预期和我得到的内容之间存在以下差异:
-
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"
在我得到的输出顺序变化的响应中,我很想知道为什么会这样,For-ex。在原始调用的响应中,第一个键是“可用的”,而在解析数据之后,它是“updated_at”
-
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.
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.id键在原始响应中有一个值36005656995,但是在使用JSON.parse将其转换为javascript对象后,id值为3.6005656995E10。由于这种转换,我无法进行后期处理。在解析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.
我希望我清楚我的查询,提前感谢通过长查询。
1 个解决方案
#1
0
When you use JSON.parse()
, the payload string is converted to an array of objects. 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).
使用JSON.parse()时,有效内容字符串将转换为对象数组。将保留*数组中项目的顺序。但是,每个数组项都是一个对象,并且不保证这些对象的属性顺序,也不需要,因为您将按名称引用这些属性(使用点表示法或括号查找)。
The id key is fine, its just being represented in scientific notation which is standard for large numbers. If you need the number in pure decimal format as a string use the toFixed()
method to convert it.
id键很好,它只是用科学记数法表示,这是大数字的标准。如果您需要纯十进制格式的数字作为字符串,请使用toFixed()方法进行转换。
#1
0
When you use JSON.parse()
, the payload string is converted to an array of objects. 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).
使用JSON.parse()时,有效内容字符串将转换为对象数组。将保留*数组中项目的顺序。但是,每个数组项都是一个对象,并且不保证这些对象的属性顺序,也不需要,因为您将按名称引用这些属性(使用点表示法或括号查找)。
The id key is fine, its just being represented in scientific notation which is standard for large numbers. If you need the number in pure decimal format as a string use the toFixed()
method to convert it.
id键很好,它只是用科学记数法表示,这是大数字的标准。如果您需要纯十进制格式的数字作为字符串,请使用toFixed()方法进行转换。