The intricate field name of a TypeScript class

I have a TypeScript class that looks like this -

export class News {
    title: string;
    snapshot: string;
    headerImage: string;
}

In my Angular service, I have a method that retrieves a list of news in the following way -

private searchNews(sortOrder : string, query? : string):Observable<News[]>{
        return this.http.get(this.url+'?'+this.buildParams(sortOrder,10,0,query))
                         .map((res:Response) => res.json())
                         .catch((error:any) => Observable.throw(error.json().error || 'Server error'))
    }

Here is an example of the JSON data I'm receiving from the server -

[{
  "jcr:path":"someurl",
  "title":"Hello News",
  "snapshot":"Here is a snapshot",
  "headerImage":"image.png"
},
 ...
]

Now, I want to add a field "path" in my News class which will map the value of the "jcr:path" field in the JSON data.

However, I can't simply write the class like this -

export class News {
    jcr:path:string;// I may write it as - path:string
    title: string;
    snapshot: string;
    headerImage: string;
}

Is there a way in TypeScript where I can instruct it to extract the value of the "path" field from the "jcr:path" properties?

Answer №1

To specify the property name, simply set it as a string value:

class Article {
    'url': string;
    // ...
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtain EmberJS Data from File in a Synchronous Manner

New to JavaScript and working on an EmberJS app. Seeking guidance on synchronously loading Ember data fixtures from a JSON file. Any tips or recommendations are greatly appreciated! ...

Error: Unable to access _rawValidators property of null object

I'm currently facing an issue with formgroup and formcontrol in Angular. When I run ng serve, it's showing an error in the console. Does anyone have a solution for this? TypeError: Cannot read properties of null (reading '_rawValidators&a ...

The watch feature is not functional when used within a modal that has been included

I am trying to incorporate a watch expression into a modal that is part of an HTML file. In my demo, I have 2 watches: one is functioning properly and the other is not. Click here for more information. Thank you ...

What is preventing me from defining the widget as the key (using keyof) to limit the type?

My expectations: In the given scenario, I believe that the C component should have an error. This is because I have set the widget attribute to "Input", which only allows the constrained key "a" of type F. Therefore, setting the value for property "b" sho ...

Error display in Elastic Apm Rum Angular implementation

Having some issues with incorporating the @elastic/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f5948598d8878098d8949b9280999487b5c7dbc4dbc4">[email protected]</a> package into my project. Angular is throwing console ...

Error encountered while retrieving an object from Akavache storage due to Newtonsoft.Json.JsonSerializationException

Storing an instance of an object in Akavache storage works fine, but retrieving it results in the following error: Newtonsoft.Json.JsonSerializationException: Unexpected token while deserializing object: EndObject. Path 'Value.Tracks[0]'. at N ...

Angular 5.2: Component type does not contain the specified property

In my Formbuilder.Group method, I have included the properties as shown in the following TypeScript code: this.form = this.fb.group({ caseNumber: ['', [Validators.required, Validators.minLength(3), Validators.maxLength(50), Val ...

Is there a way to detect modifications in a JSON API using Django or Android?

I've integrated djangorestframework for my api and am currently developing an Android App that interacts with the api by fetching and posting data. I'm interested in finding a way to detect changes in json data from the api. For example, in a cha ...

Merging the functionalities of ons-navigator with ng-repeat

My goal is to set up an ons-navigator component with multiple pages generated from an array using ng-repeat or a similar method. Additionally, I need a way to navigate forward through these pages. I attempted to use ng-repeat on the ons-template element w ...

What is the best way to access a property within a typescript object?

I'm working with the following code snippet: handleSubmit() { let search = new ProductSearch(); search = this.profileForm.value; console.log(search); console.log(search.code); } When I run the console.log(search) line, it outputs: ...

Is it possible to conceal dom elements within an ng-template?

Utilizing ng-bootstrap, I am creating a Popover with HTML and bindings. However, the ng-template keeps getting recreated every time I click the button, causing a delay in the initialization of my component. Is there a way to hide the ng-template instead? ...

Filtering rows in JQgrid is made easy after the addition of a new record

Here's the situation I'm facing: Every second, my script adds a new record using the "setInterval" function: $("#grid").jqGrid('addRowData', id, data, 'first').trigger("reloadGrid"); However, when users apply filters while t ...

Protractor encounters a TypeError when attempting to navigate with Firefox version 59 due to a cyclic object value

Our team has implemented several Protractor tests for our Angular JS application. Recently, we considered upgrading the Firefox browser to version 59 while using Selenium 3.11.0. However, after the upgrade, whenever we try to use element(by. in our tests ...

Tips for calculating the total of an array's values

I am seeking a straightforward explanation on how to achieve the following task. I have an array of objects: const data = [ { "_id": "63613c9d1298c1c70e4be684", "NameFood": "Coca", "c ...

When transitioning to a different page, Angular is creating a duplicate of $scope

My webpage features a section where I showcase all the individuals stored in the database, referred to as the All Persons Page. Within this page, there are two primary options: Add New Person Delete Person (Any selected individual) An issue arises when ...

Validation of JSON Schemas using System.Text.Json

Is there any support in System.Text.Json for validating Json schemas? I've checked the documentation but it seems like this feature is still in progress. ...

Converting PDF files to JSON in ASP.NET and transferring them to AngularJS using Base64 does not result in identical files

After encoding a PDF file as Base64 and assigning it to a property of my model object in ASP.NET WEB API, the JSON response is received by my AngularJS client application. However, upon saving the file, I noticed that the file size increases by one and 1/3 ...

Task: Convert SQL query results from XML to JSON format using Logic App

I have encountered an issue with my SQL query that returns XML data in SQL Server using For XML. When I run the query (Execute a SQL Query) in Logic Apps, it converts the XML into JSON format. My goal is to send this XML data to a Dynamics 365 integration ...

Incorporate NodeJs middleware to define headers

Looking to enhance my middleware by including a refresh token in the header, inspired by the solution provided in the first response of this query: implementing refresh-tokens with angular and express-jwt Middleware implementation on the backend: jwt.ver ...

Arranging elements in a list according to their position on the canvas using AngularJS

I am currently working on drawing rectangles on an html5 canvas using the JSON format provided below. My goal is to sort the array based on the x and y locations of each element. { "obj0": { "outerRects": [ { "outerRectRoi": { "x1": 0, " ...