Tips for showing JSON information in Nativescript

How can I display all values in a page using the provided JSON data?

res {
   "StatusCode": 0,
   "StatusMessage": "OK",
   "StatusDescription": [
     {
       "sensors": [
         {
           "serial": "sensor1",
           "id": "1"
         },
         {
           "serial": "sensor2",
           "id": "2"
         },
         {
           "serial": "sensor3",
           "id": "3"
         }
       ],
       "HBP_id": "12",
       "HB_id": "123",
       "serial_number": "hb1",
       "note": "test"
    }
   ]
}

I need to display the following in Nativescript:

serial_number: hb1
sensors : sensor1

I attempted to use the code below, but it only displays the serial number:

<StackLayout class="page">
    <ListView [items]="items" class="list-group">
         <ng-template let-item="item">
             <Label [text]="item.serial_number"
              class="list-group-item"></Label>
         </ng-template>
    </ListView>
</StackLayout>

Does anyone have an idea on how I can also display the sensors along with the serial number?

Thank you

Solution:

<ListView [items]="items" (itemTap)="onItemTap($event)">
                <ng-template let-item="item" let-i="index" let-odd="odd" let-even="even">
                    <StackLayout [class.odd]="odd" [class.even]="even">
                        <Label [text]="item.serial_number"></Label>
                        <Label *ngFor="let subItem of item?.sensors" [text]="subItem.serial"></Label>
                    </StackLayout>
                </ng-template>
            </ListView>

Answer №1

Have you considered utilizing nested <ng-template> elements?

<StackLayout class="page">
    <ListView [items]="items" class="list-group">
         <ng-template let-item="item">
             <Label [text]="item.serial_number"
              class="list-group-item"></Label>
             <ng-template *ngFor="let subItem of item?.sensors">
                  <Label [text]="subItem.serial"
                      class="list-group-item"></Label>
             </ng-template>
         </ng-template>
    </ListView>
</StackLayout>

If using ngFor is not an option, you can try using let-subItem="item?.sensors".

This approach might be useful for achieving your desired outcome!

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

Verify the anticipated URL and showcase the real URL

During a functional test, I am attempting to create a "Then" step where the current URL is verified. After researching on SO, it appears that the proper approach is to wait for the URL to match the expected one: Then('The URL contains {string}' ...

"Troubleshooting the issue of nested jQuery Ajax requests failing to execute properly

UPDATE: I'm getting an error saying that my li tag is considered an illegal token. I'm unsure how to resolve this issue as I believed I was appending it within a ul tag I'm tasked with fetching a JSON array of public Github repositories and ...

Using Python to make a single API request that creates multiple records at once by using

Using Python 3 alongside an API to send a single JSON payload has been effective, but slow due to the need to send 200 items regularly: { "first_name":"Ellen", "address_country":"US" } Manually sending this payload creates two records, which is the d ...

Changing an element within an item stored in Ionic Storage

Hello, I am currently attempting to update a specific part of an object stored in Ionic storage. The current data in the Storage looks like this: key : object value : {a: "1", b: "2", c: "3"} To modify one of the values to 10, I created the following fu ...

Fetching JSON data from a URL and displaying it in a TextView

How can I parse and display this JSON data in Android Studio? I want each item to be shown individually in a textview. Thank you all for your help! The content is from a URL only. {"s":true,"code":0,"errors":[],"c":"2.54","y":"5.8","i":"2.9","x":"0"} ...

Accessing S3 bucket contents in Angular using Observables

Looking for guidance on structuring a service method in Angular4 to create an s3.listObjects call and return the contents of an S3 bucket as an Observable. Here is my current attempt, unfortunately not yielding successful results: public retrieveFilesFro ...

What is the best way to showcase an element within an array that matches a particular value or character?

I have obtained a JSON array from hxxp://best1st.info/Moviedb/json.php?m=tt2015381&o=json Here is a snippet of the array output: . and so on . . [STORYLINE] => On planet Earth in 1988, young Peter Quill ( ) sits in the waiting room of a hospital. ...

Angular ngrx store not updating with the most recent value

When navigating to an "Account Detail" page and passing a parameter, it triggers a call to the Web Api for fetching account details. The issue arises when loading the "Account Detail" page for the first time - nothing is displayed. Subsequently, switching ...

Best location to define numerous dialog components

Currently, I have 8 custom Modals that are called in various places within my app. These modals are currently located inside the app.component.html as shown below: <agc class="app-content" [rows]="'auto 1fr'" [height]=" ...

Trouble with parsing JSON in PHP

I've been encountering issues with a PHP script I recently created. The problem lies in my inability to retrieve data from a JSON file named demo.json using PHP. Below is the content of the JSON file: { "checkouts":[ { "billing_address":{ ...

Interacting with PHP through JavaScript function calls

I am facing some frustration with my website due to an issue involving PHP. When I retrieve data from a PHP file, it returns a JSON list like this: {"Data":{"Recipes":{"Recipe_5":{"ID":"5","TITLE":"Spaghetti Bolognese"},"Recipe_7":{"ID":"7","TITLE":"Wurst ...

Instructions for transferring numerous chosen files (such as .doc, .pdf, .jpeg, .png) to a server from an Android device

Can you provide guidance on selecting multiple files from a file manager in Android? Once the details of the image are displayed (such as image size, name, and the option to delete the files), how can we upload these files to a server? ...

What is the best way to utilize derived types in TypeScript?

Given object A: interface A { boolProp: boolean, stringProp: string, numberProp: number, ...other props/methods... } Now, I need objects that contain one of the properties from A and set a default value for it, with other properties being irre ...

Is it possible to silence the other person's audio using livekit?

As a developer, I create meeting apps using React and livekit technology. I am looking for a reliable method to silence the audio of other participants in the virtual room. Any suggestions? ...

JavaScript and JSON interchangeably, the first AJAX response should be rewritten by the second response

I am facing an issue with my code: I have two ajax calls being made on window.load, and it seems like the response from the second AJAX call is overwriting the response from the first one before my function can process it. I'm not sure where I'm ...

Swift4 JSON Parsing Tool

Looking to convert a JSON file into a Codable struct in Swift4 for iOS11.1 and Xcode9.1, Below is my code: struct Station: Codable { let htmlAttributions: [String] let nextPageToken: String let status: String struct Result: Codable { ...

What steps should be taken to address issues with JsonSchemaValidator?

Having trouble with the JsonSchemaValidator. Can someone assist me in resolving this issue, or is it potentially a bug within the library? Here is my test code: Response resp = given().cookie(cookie).when().get(getEndpointLink("link_menuItems")); resp.the ...

Child route CanActivate guards execute before the parent Resolve completes

I'm currently facing an issue with resolving data prior to navigating to child routes in order to utilize that data within the children guard. The problem lies in the fact that the parent resolver is executing after the child guard has already been tr ...

Error message stating: "Form control with the name does not have a value accessor in Angular's reactive forms."

I have a specific input setup in the following way: <form [formGroup]="loginForm""> <ion-input [formControlName]="'email'"></ion-input> In my component, I've defined the form as: this.log ...

What is the correct way to forcefully override an existing type in TypeScript?

As I work with Formik, a React form library, I find myself creating custom fields and utilizing the generic Schema type provided by Formik. This type represents the values object, which holds all the values for each field in the form. One of the custom co ...