2

I get this JSON from a server:

"Body": {
    "ErrorsContent": {
        "FunctionalError": [
            {
                "Code": "110900",
                "Error": "xx",
                "Position": "xx",
                "Value": ""
            },
            {
                "Code": "110900",
                "Error": "xx",
                "Position": "xx",
                "Value": ""
            },
            {
                "Code": "110900",
                "Error": "xx",
                "Position": "xx",
                "Value": ""
            },
            {
                "Code": "110902",
                "Error": "xx",
                "Position": "xx",
                "Value": ""
            },
            {
                "Code": "110900",
                "Error": "xx",
                "Position": "xx",
                "Value": ""
            }
        ]
    }
}

I need it to parse to an Angular object, so I created a couple classes:

export class Body{
    public ErrorsContent: ErrorsContent;
}

class ErrorsContent{
    public FunctionalError: FunctionalError;
}

class FunctionalError{
    public Code: any;
    public Error: any;
    public Position: any;
    public Value: any;
}

But this does not work fine when I have more than one FuntionalError.

How can I set the FuntionalError class so that I can have more than one error without being a class itself?

Any advice is welcome! Thanks.

4
  • 2
    The answer below is right, but I would also suggest you to 1) send your number values like Code as numbers and not as strings, 2) don't use the : any type. Give it the right one → In your case string or if you follow tip 1) also number Commented Sep 25, 2019 at 10:41
  • 1
    Use online helpers like jsontots.com to convert server responses to ts classes and avoid errors. Commented Sep 25, 2019 at 10:42
  • @MauriceNino +1 also properties name should start with lower case so for example Code should be code in classes as well as from server response Commented Sep 25, 2019 at 10:44
  • Thanks for the advices, but I receive the JSON from a remote server so I have nothing to do with that :) @MauriceNino Commented Sep 25, 2019 at 10:45

3 Answers 3

4

Change your ErrorsContent class to

class ErrorsContent{
    public FunctionalError: FunctionalError[];//it should be array 
}
Sign up to request clarification or add additional context in comments.

2 Comments

What if the JSON comes with one FuntionalError and it is not inside [ ]?
@Iñigo i will not suggest you to do that there must be consistent format.For example you execute a foreach and you get and object instead of array your code will break
3

FunctionalError is an array. You are declaring it as a object.

Use

class ErrorsContent {
    public FunctionalError: FunctionalError[];
}

instead of

class ErrorsContent {
    public FunctionalError: FunctionalError;
}

You can use any online tool to convert server JSON response to TS classes. Ex: jsontots, json2ts etc.

Comments

2

Try like this:

class ErrorsContent{
    public FunctionalError: Array<FunctionalError>;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.