Need to get the value of my json in html

14 views
Skip to first unread message

Sandeep Shukla

unread,
Jun 23, 2020, 11:09:56 AM6/23/20
to Angular and AngularJS discussion
Hi,
My json is 
{
    "error": null,
    "document": {
        "title": "Hello World Angular",
        "heading": "subject to changes"
    },
    "versions": [
        {
            "attributes": {
                "sequence": "1",
                "versionNumber": "1.6",
                "id": "A00100"
            },
            "contentElements": [
                {
                    "contentType": "BASE",
                    "mediaType": "text/xml",
                    "fileName": null,
                    "size": 779,
                    "content": "This is content"
                },
                {
                    "contentType": "NOTELOG",
                    "mediaType": "text/plain",
                    "fileName": null,
                    "size": 29,
                    "content": "Content2"
                }
            ]
        }
    ]
} How do i get the values in html under attributes,contentElements. I am unable to get it. Any help would be appreciated. I can handle simple json but this is something not working for me. Regards Sandeep Shukla

Manjari Singh

unread,
Jun 23, 2020, 11:59:20 AM6/23/20
to ang...@googlegroups.com
You have to index from versions , as per your current Json
Attributes , does not include cont...

--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/angular/279d78ce-1cc3-45fe-a9cc-ea57eedacc9fo%40googlegroups.com.
--
Sent from Gmail Mobile

sandeep...@gmail.com

unread,
Jun 24, 2020, 3:12:55 AM6/24/20
to Angular and AngularJS discussion
Thanks Manjari for your response. But it is not clear to me.
in html I am saving json in result object like this in my TS file.
         this.result = JSON.parse(JSON.stringify(data));
In html I am trying to access
     <div> {{result.document.title}} </div>   This works fine 
 <div> {{result.versions.attribute.versionnumber}} </div>     I dont get this value . Please look intot he structure of json

Please share me what I am supposed to use to get correct reult. It is something related to traversing to the json object which I am not sure how to do it.

Regards
SandeepS hukla

Manjari Singh

unread,
Jun 24, 2020, 4:08:57 AM6/24/20
to ang...@googlegroups.com
Versions is an array, [],content elements also array, index it, beginning with 0,1.., you can play around on w3 schools json tutorial, try it, for practice, and then correct your accessing 

Sandeep Shukla

unread,
Jun 24, 2020, 4:28:53 AM6/24/20
to ang...@googlegroups.com
I had already tried,does not ring a bell and I am not very good at frontend :)

Already spent a week , I have developed the whole angular project but this I have encountered for the first time.

May be if you could let me know the solution then I can build concept around it.

Regards
Sandeep Shukla



--
Regards
Sandeep Shukla,
Mobile# +31647969951

NANA DARKO

unread,
Jun 24, 2020, 4:54:23 AM6/24/20
to ang...@googlegroups.com
You can do this in your view or template:
It will loop through the array displaying  the data on the ui


<div *ngFor="let version of result.versions">
<span> {{ version?.attributes?.id }} </span>
<span> {{ version?.attributes?.sequence }} </span>
</div>

Sandeep Shukla

unread,
Jun 24, 2020, 6:06:51 AM6/24/20
to ang...@googlegroups.com
Thanks Nana. I got the values under attributes but under contenElements I dont get the values. Can I not reuse the same code as yours like this to get the data under contentElements
  <div *ngFor="let version of result.versions">                          
    <span> {{ version?.attributes?.id }} </span>                       
    <span> {{ version?.attributes?.sequence }} </span>
    <span> {{ version?.contentElements?.contentType }} </span> This does not work for me
  </div>

Regards
Sandeep Shukla

NANA DARKO

unread,
Jun 24, 2020, 7:12:43 AM6/24/20
to ang...@googlegroups.com
// contentElements is also an array and should be treated as such
// The structure below should work for you

<div *ngFor="let version of result.versions">                          
    <span> {{ version?.attributes?.id }} </span>                       
    <span> {{ version?.attributes?.sequence }} </span>
    <ul>
        <li *ngFor="let element of version.contentElements">
         {{ element?.contentType }}
        </li>
    </ul>
  </div>

Sandeep Shukla

unread,
Jun 24, 2020, 7:53:14 AM6/24/20
to ang...@googlegroups.com
Thanks Nana. It worked and it helped me to build the concept.

Regards
Sandeep Shukla

NANA DARKO

unread,
Jun 24, 2020, 7:57:46 AM6/24/20
to ang...@googlegroups.com
The pleasure is mine.
All the best.


Reply all
Reply to author
Forward
0 new messages