简体   繁体   English

如何访问下面的json值

[英]How to access the below json value

Hi I'm trying to access the value DepartmentId but getting error as value doesn't exists.嗨,我正在尝试访问值DepartmentId但由于值不存在而出错。

I can get the values inside the curly braces when I try to access the DepartmentId , I'm getting error.当我尝试访问DepartmentId ,我可以获取大括号内的值,但出现错误。

Below is my map function下面是我的地图功能

 r.PrimarySearchResults.map((value) => {
console.log(value)}
)

I can get value.Rank but not value.DepartmentId我可以得到value.Rank但不value.DepartmentId

Below is the JSON object I got.下面是我得到的 JSON 对象。 This is when I expand the Object.这是我展开 Object.

{Rank: "16.9111518859863", DocId: 
"17598046715456", Title: "HubSite", SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2", 
WebTemplate: "SITEPAGEPUBLISHING", …}
Culture: "en-US"
DepartmentId: "{3d408bfe-9172-4df5-b36e-863c066e9ada}"
DocId: "17598046715456"
PartitionId: "51ddbb65-42e8-4906-82e4-8d97c6626ef7"
Rank: "16.9111518859863"
RenderTemplateId: "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js"
ResultTypeId: "0"
SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2"
SiteId: "3d408bfe-9172-4df5-b36e-863c066e9ada"
Title: "HubSite"
UniqueId: "{F08C7BCE-C886-4A09-AA22-D66879DD5252}"
UrlZone: "0"
WebId: "edec632a-5671-49bd-a7fe-27a6e851f09a"
WebTemplate: "SITEPAGEPUBLISHING"
__proto__: Object

Find the below image找到下面的图片请找到图片

Tried arranging your JSON object, seems that DepartmentId is not included in the JSON尝试安排您的 JSON 对象,似乎 DepartmentId 未包含在 JSON 中

{
    Rank: "16.9111518859863", 
    DocId: "17598046715456", 
    Title: "HubSite", 
    SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2", 
    WebTemplate: "SITEPAGEPUBLISHING", }

Culture: "en-US" DepartmentId: "{3d408bfe-9172-4df5-b36e-863c066e9ada}" DocId: "17598046715456" PartitionId: "51ddbb65-42e8-4906-82e4-8d97c6626ef7" Rank: "16.9111518859863" RenderTemplateId: "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js" ResultTypeId: "0" SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2" SiteId: "3d408bfe-9172-4df5-b36e-863c066e9ada" Title: "HubSite" UniqueId: "{F08C7BCE-C886-4A09-AA22-D66879DD5252}" UrlZone: "0" WebId: "edec632a-5671-49bd-a7fe-27a6e851f09a" WebTemplate: "SITEPAGEPUBLISHING"

Try this:尝试这个:

var value = JSON.parse(value); var value = JSON.parse(value); console.log(value.DepartmentId); console.log(value.DepartmentId);

As mentioned above, the full JSON object being returned should be able to be parsed into an object and then gotten.如上所述,返回的完整 JSON 对象应该能够解析为一个对象然后获取。

If your response object is PrimarySearchResults , you should be able to do:如果您的响应对象是PrimarySearchResults ,您应该能够:

r.PrimarySearchResults.DepartmentId;
// should return {3d408bfe-9172-4df5-b36e-863c066e9ada}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM