admin 管理员组文章数量: 1086019
I am retrieving data from the Google Places API and casting it directly into objects for use elsewhere.
The problem is that sometimes the responses do not have all of the fields, so this causes my app to crash when the objects can't properly be created due to missing fields in the JSON response.
Here is an example of what I am talking about:
say the format of a response for example is this:
{ "a" : "some_str", "b" : "some_str", "c" : "some_str" }
but once in a while field "b" will be missing, so the response looks like this:
{ "a" : "some_str", "c" : "some_str" }
How can I account for this when I try to parse the JSON data into objects?
For example, here is a code that I would use to parse the data:
this.http.get(URL).subscribe(details => {
let detailsObj = details.json();
let myObj: SomeObject = {
"fieldA" : detailsObj.a,
"fieldB" : detailsObj.b,
"fieldC" : detailsObj.c,
}
});
If the field "b" doesn't exist on the JSON response, the value will be undefined on the "detailsObj" which causes a runtime error as it is trying to create an object with an undefined field.
How can I still create the object despite the partially undefined data? Ideally the data that is missing could be filled with a null.
Any pointers in the right direction would be greatly appreciated, thank you for your time!
I am retrieving data from the Google Places API and casting it directly into objects for use elsewhere.
The problem is that sometimes the responses do not have all of the fields, so this causes my app to crash when the objects can't properly be created due to missing fields in the JSON response.
Here is an example of what I am talking about:
say the format of a response for example is this:
{ "a" : "some_str", "b" : "some_str", "c" : "some_str" }
but once in a while field "b" will be missing, so the response looks like this:
{ "a" : "some_str", "c" : "some_str" }
How can I account for this when I try to parse the JSON data into objects?
For example, here is a code that I would use to parse the data:
this.http.get(URL).subscribe(details => {
let detailsObj = details.json();
let myObj: SomeObject = {
"fieldA" : detailsObj.a,
"fieldB" : detailsObj.b,
"fieldC" : detailsObj.c,
}
});
If the field "b" doesn't exist on the JSON response, the value will be undefined on the "detailsObj" which causes a runtime error as it is trying to create an object with an undefined field.
How can I still create the object despite the partially undefined data? Ideally the data that is missing could be filled with a null.
Any pointers in the right direction would be greatly appreciated, thank you for your time!
Share Improve this question asked Jan 23, 2018 at 6:15 BiggytinyBiggytiny 52910 silver badges29 bronze badges 1- Your app wont crash here. It will pass undefined. – Jonas Wilms Commented Jan 23, 2018 at 6:19
3 Answers
Reset to default 3Use ||
to fill in null
when b
is missing:
"fieldB" : detailsObj.b || null,
or be more robust and specifically check for undefined
:
"fieldB" : detailsObj.b === undefined ? null : detailsObj.b,
The first option is more concise and easier to read, but it will set the value to null
if b
is 0
or false
or some other falsey value.
Have you tried the LoDash _.get function? You can do
Const _ = require("lodash");
"fieldA": _.get(detailsObj, "a", null);
you can try this :
interface SampleInterface {
a ?: string;
b ?: string;
c ?: string;
}
var obj = { "a" : "some_str1", "c" : "some_str3" };
sampleInterface : SampleInterface = obj;
console.log(SampleInterface.b);
本文标签: javascriptHow to handle missing JSON data when creating objects (Typescript)Stack Overflow
版权声明:本文标题:javascript - How to handle missing JSON data when creating objects? (Typescript) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744099391a2533427.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论