admin管理员组

文章数量:1394554

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
Add a ment  | 

3 Answers 3

Reset to default 3

Use || 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